-
Notifications
You must be signed in to change notification settings - Fork 7
/
readPTU_FLIM.py
585 lines (438 loc) · 27.9 KB
/
readPTU_FLIM.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
#!/usr/bin/env python
# coding: utf-8
# Read PTU Library and FLIM data
# Author: Sumeet Rohilla
# sumeetrohilla@gmail.com
# PTU Reader Library
"""
Created on Tue, 14 May 2019
Updated on Sun, 20 Dec, 2020
@author: SumeetRohilla, sumeetrohilla@gmail.com
"Good artists copy; great artists steal."
Largely inspired from examples:
- PicoQuant demo codes
https://github.com/PicoQuant/PicoQuant-Time-Tagged-File-Format-Demos
- from a jupyter notebook by tritemio on GitHub:
https://gist.github.com/tritemio/734347586bc999f39f9ffe0ac5ba0e66
Aim : Open and convert Picoquant .ptu image files for FLIM analysis
* Use : Simply select your .ptu file and library will provide:
* 1) Lifetime image stack for each channel (1-8).
* flim_data_stack = [numPixX numPixY numDetectors numTCSPCbins]
* Fluorescence decays in each pixel/num_detection_channel are available for the whole acquisition
* Frame-wise info is not implemented here, but in principle is pretty straightforwad to implement
* 2) Intensity is just acquisition flim_data_stack(in photons) is accessed by binning across axis = numTCSPCbins and numDetectors
* 3) get_flim_data_stack class method is numba accelarated (using @jit decorator) to gain speed in building flim_data_stack from raw_tttr_data
"""
import time
import sys
import struct
import io
import numpy as np
from collections import OrderedDict, namedtuple
from sys import getsizeof
import gc
import matplotlib.pyplot as plt
from numba import njit, jit
from numba.experimental import jitclass
from numba.types import uint16
@njit
def get_flim_data_stack_static(sync, tcspc, channel, special, header_variables):
ImgHdr_Ident = header_variables[0]
MeasDesc_Resolution = header_variables[1]
MeasDesc_GlobalResolution = header_variables[2]
ImgHdr_PixX = header_variables[3]
ImgHdr_PixY = header_variables[4]
ImgHdr_LineStart = header_variables[5]
ImgHdr_LineStop = header_variables[6]
ImgHdr_Frame = header_variables[7]
if (ImgHdr_Ident == 9) or (ImgHdr_Ident == 3):
tcspc_bin_resolution = 1e9*MeasDesc_Resolution # in Nanoseconds
sync_rate = np.ceil(MeasDesc_GlobalResolution*1e9) # in Nanoseconds
# num_of_detectors = np.max(channel)+1
# num_tcspc_channel = np.max(tcspc)+1
# num_tcspc_channel = floor(sync_rate/tcspc_bin_resolution)+1
num_of_detectors = np.unique(channel).size
num_tcspc_channel = np.unique(tcspc).size
num_pixel_X = ImgHdr_PixX
num_pixel_Y = ImgHdr_PixY
flim_data_stack = np.zeros((num_pixel_Y, num_pixel_X, num_of_detectors,num_tcspc_channel), dtype = np.uint16)
# Markers necessary to make FLIM image stack
LineStartMarker = 2**(ImgHdr_LineStart-1)
LineStopMarker = 2**(ImgHdr_LineStop-1)
FrameMarker = 2**(ImgHdr_Frame-1)
# Get Number of Frames
FrameSyncVal = sync[np.where(special == FrameMarker)]
num_of_Frames = FrameSyncVal.size
read_data_range = np.where(sync == FrameSyncVal[num_of_Frames-1])[0][0]
L1 = sync[np.where(special == LineStartMarker)] # Get Line start marker sync values
L2 = sync[np.where(special == LineStopMarker)] # Get Line start marker sync values
syncPulsesPerLine = np.floor(np.mean(L2[10:] - L1[10:]))
# Get pixel dwell time values from header for PicoQuant_FLIMBee or Zeiss_LSM scanner
# if 'StartedByRemoteInterface' in head.keys():
# #syncPulsesPerLine = round((head.TimePerPixel/head.MeasDesc_GlobalResolution)*num_pixel_X);
# syncPulsesPerLine = np.floor(np.mean(L2[10:] - L1[10:]))
# else:
# #syncPulsesPerLine = floor(((head.ImgHdr_TimePerPixel*1e-3)/head.MeasDesc_GlobalResolution)*num_pixel_X);
# syncPulsesPerLine = np.floor(np.mean(L2[10:] - L1[10:]))
# Initialize Variable
currentLine = 0
currentSync = 0
syncStart = 0
currentPixel = 0
unNoticed_events = 0
countFrame = 0
insideLine = False
insideFrame = False
isPhoton = False
for event in range(read_data_range+1):
if num_of_Frames == 1:
# when only zero/one frame marker is present in TTTR file
insideFrame = True
currentSync = sync[event]
special_event = special[event]
# is the record a valid photon event or a special marker type event
if special[event] == 0:
isPhoton = True
else:
isPhoton = False
if not(isPhoton):
#This is not needed once inside the first Frame marker
if (special_event == FrameMarker):
insideFrame = True
countFrame += 1
currentLine = 0
if (special_event == LineStartMarker):
insideLine = True
syncStart = currentSync
elif (special_event == LineStopMarker):
insideLine = False
currentLine += 1
syncStart = 0
if (currentLine >= num_pixel_Y):
insideFrame = False
currentLine = 0
# Build FLIM image data stack here for N-spectral/tcspc-input channels
if (isPhoton and insideLine and insideFrame):
currentPixel = int(np.floor((((currentSync - syncStart)/syncPulsesPerLine)*num_pixel_X)))
tmpchan = channel[event]
tmptcspc = tcspc[event]
if (currentPixel < num_pixel_X) and (tmptcspc<num_tcspc_channel):
flim_data_stack[currentLine][currentPixel][tmpchan][tmptcspc] +=1
# else:
# print("Piezo Scanner Data Reader Not Implemented Yet!!! \n")
return flim_data_stack
class PTUreader():
"""
PTUreader() provides the capability to retrieve raw_data or image_data from a PTU file acquired using available PQ TCSPC module in the year 2019
Input arguements:
filename= path + filename
print_header = True or False
Output
ptu_read_raw_data = This function reads single-photon data from the file 'name'
The output variables contain the followig data:
sync : number of the sync events that preceeded this detection event
tcspc : number of the tcspc-bin of the event
channel : number of the input channel of the event (detector-number)
special : marker event-type (0: photon; else : virtual photon/line_Startmarker/line_Stopmarker/framer_marker)
For example: Please see Jupyter notebook for additional info on how to get raw TTTR data
get_flim_data_stack = This function builds a FLIM image from raw tttr_data
Outputs: flim_data_stack = [numPixX numPixY numDetectors numTCSPCbins]
"""
# Global constants
# Define different tag types in header
tag_type = dict(
tyEmpty8 = 0xFFFF0008,
tyBool8 = 0x00000008,
tyInt8 = 0x10000008,
tyBitSet64 = 0x11000008,
tyColor8 = 0x12000008,
tyFloat8 = 0x20000008,
tyTDateTime = 0x21000008,
tyFloat8Array = 0x2001FFFF,
tyAnsiString = 0x4001FFFF,
tyWideString = 0x4002FFFF,
tyBinaryBlob = 0xFFFFFFFF,
)
# Dictionary with Record Types format for different TCSPC devices and corresponding T2 or T3 TTTR mode
rec_type = dict(
rtPicoHarpT3 = 0x00010303, # (SubID = $00 ,RecFmt: $01) (V1), T-Mode: $03 (T3), HW: $03 (PicoHarp)
rtPicoHarpT2 = 0x00010203, # (SubID = $00 ,RecFmt: $01) (V1), T-Mode: $02 (T2), HW: $03 (PicoHarp)
rtHydraHarpT3 = 0x00010304, # (SubID = $00 ,RecFmt: $01) (V1), T-Mode: $03 (T3), HW: $04 (HydraHarp)
rtHydraHarpT2 = 0x00010204, # (SubID = $00 ,RecFmt: $01) (V1), T-Mode: $02 (T2), HW: $04 (HydraHarp)
rtHydraHarp2T3 = 0x01010304, # (SubID = $01 ,RecFmt: $01) (V2), T-Mode: $03 (T3), HW: $04 (HydraHarp)
rtHydraHarp2T2 = 0x01010204, # (SubID = $01 ,RecFmt: $01) (V2), T-Mode: $02 (T2), HW: $04 (HydraHarp)
rtTimeHarp260NT3 = 0x00010305, # (SubID = $00 ,RecFmt: $01) (V1), T-Mode: $03 (T3), HW: $05 (TimeHarp260N)
rtTimeHarp260NT2 = 0x00010205, # (SubID = $00 ,RecFmt: $01) (V1), T-Mode: $02 (T2), HW: $05 (TimeHarp260N)
rtTimeHarp260PT3 = 0x00010306, # (SubID = $00 ,RecFmt: $01) (V1), T-Mode: $03 (T3), HW: $06 (TimeHarp260P)
rtTimeHarp260PT2 = 0x00010206, # (SubID = $00 ,RecFmt: $01) (V1), T-Mode: $02 (T2), HW: $06 (TimeHarp260P)
rtMultiHarpNT3 = 0x00010307, # (SubID = $00 ,RecFmt: $01) (V1), T-Mode: $03 (T3), HW: $07 (MultiHarp150N)
rtMultiHarpNT2 = 0x00010207, # (SubID = $00 ,RecFmt: $01) (V1), T-Mode: $02 (T2), HW: $07 (MultiHarp150N)
)
def __init__(self, filename, print_header_data = False):
# raw_tttr_data = False, get_image_data = True
# if get_image_data = True then get_raw_data = False
# else get_raw_data = True and get_image_data = False
# Usually a user will only demand for raw or image data
#Reverse mappins of tag-type and record-type dictionary
self.tag_type_r = {j: k for k, j in self.tag_type.items()}
self.rec_type_r = {j: k for k, j in self.rec_type.items()}
self.ptu_name = filename
self.print_header = print_header_data
f = open(self.ptu_name, 'rb')
self.ptu_data_string = f.read() # ptu_data_string is a string of bytes and reads all file in memory
f.close()
#Check if the input file is a valid input file
# Read magic and version of the PTU File
self.magic = self.ptu_data_string[:8].rstrip(b'\0')
self.version = self.ptu_data_string[8:16].rstrip(b'\0')
if self.magic != b'PQTTTR':
raise IOError("This file is not a valid PTU file. ")
exit(0)
self.head = {}
# Read and print header if set True
self._ptu_read_head(self.ptu_data_string)
# Read and return Raw TTTR Data
self._ptu_read_raw_data()
if self.print_header == True:
return self._print_ptu_head()
return None
def _ptu_TDateTime_to_time(self, TDateTime):
EpochDiff = 25569 # days between 30/12/1899 and 01/01/1970
SecsInDay = 86400 # number of seconds in a day
return (TDateTime - EpochDiff) * SecsInDay
def _ptu_read_tags(self, ptu_data_string, offset):
# Get the header struct as a tuple
# Struct fields: 32-char string, int32, uint32, int64
tag_struct = struct.unpack('32s i I q', ptu_data_string[offset:offset+48])
offset += 48
# Get the tag name (first element of the tag_struct)
tagName = tag_struct[0].rstrip(b'\0').decode()
keys = ('idx', 'type', 'value')
tag = {k: v for k, v in zip(keys, tag_struct[1:])}
# Recover the name of the type from tag_dictionary
tag['type'] = self.tag_type_r[tag['type']]
tagStringR='NA'
# Some tag types need conversion to appropriate data format
if tag['type'] == 'tyFloat8':
tag['value'] = np.int64(tag['value']).view('float64')
elif tag['type'] == 'tyBool8':
tag['value'] = bool(tag['value'])
elif tag['type'] == 'tyTDateTime':
TDateTime = np.uint64(tag['value']).view('float64')
t = time.gmtime(self._ptu_TDateTime_to_time(TDateTime))
tag['value'] = time.strftime("%Y-%m-%d %H:%M:%S", t)
# Some tag types have additional data
if tag['type'] == 'tyAnsiString':
try: tag['data'] = ptu_data_string[offset: offset + tag['value']].rstrip(b'\0').decode()
except: tag['data'] = ptu_data_string[offset: offset + tag['value']].rstrip(b'\0').decode(encoding = 'utf-8', errors = 'ignore')
tagStringR=tag['data']
offset += tag['value']
elif tag['type'] == 'tyFloat8Array':
tag['data'] = np.frombuffer(ptu_data_string, dtype='float', count=tag['value']/8)
offset += tag['value']
elif tag['type'] == 'tyWideString':
# WideString default encoding is UTF-16.
tag['data'] = ptu_data_string[offset: offset + tag['value']*2].decode('utf16')
tagStringR=tag['data']
offset += tag['value']
elif tag['type'] == 'tyBinaryBlob':
tag['data'] = ptu_data_string[offset: offset + tag['value']]
offset += tag['value']
tagValue = tag['value']
return tagName, tagValue, offset, tagStringR
def _ptu_read_head(self, ptu_data_string):
offset = 16
FileTagEnd = 'Header_End'
tag_end_offset = ptu_data_string.find(FileTagEnd.encode())
tagName, tagValue, offset, tagString = self._ptu_read_tags(ptu_data_string, offset)
self.head[tagName] = tagValue
#while offset < tag_end_offset:
while tagName != FileTagEnd:
tagName, tagValue, offset, tagString = self._ptu_read_tags(ptu_data_string, offset)
if tagString=='NA': self.head[tagName] = tagValue
else: self.head[tagName] = tagString
# print(tagName, tagValue)
# End of Header file and beginning of TTTR data
self.head[FileTagEnd] = offset
def _print_ptu_head(self):
#print "head" dictionary
print("{:<30} {:8}".format('Head ID','Value'))
for keys in self.head:
val = self.head[keys]
print("{:<30} {:<8}".format(keys, val))
def _ptu_read_raw_data(self):
'''
This function reads single-photon data from the file 's'
Returns:
sync : number of the sync events that preceeded this detection event
tcspc : number of the tcspc-bin of the event
chan : number of the input channel of the event (detector-number)
special : indicator of the event-type (0: photon; else : virtual photon)
num : counter of the records that were actually read
'''
record_type = self.rec_type_r[self.head['TTResultFormat_TTTRRecType']]
num_T3records = self.head['TTResult_NumberOfRecords']
#Read all T3 records in memory
t3records = np.frombuffer(self.ptu_data_string, dtype='uint32', count=num_T3records, offset= self.head['Header_End'])
# Clear ptu string data from memory and delete it's existence
del self.ptu_data_string
#Next is to do T3Records formatting according to Record_type
if record_type == 'rtPicoHarpT3':
print('TCSPC Hardware: {}'.format(record_type[2:]))
# +----------------------+ T3 32 bit record +---------------------+
# |x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x| |x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x| --> 32 bit record
# +-------------------------------+ +-------------------------------+
# +-------------------------------+ +-------------------------------+
# | | | | | | | | | | | | | | | | | |x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x| --> Sync
# +-------------------------------+ +-------------------------------+
# +-------------------------------+ +-------------------------------+
# | | | | |x|x|x|x|x|x|x|x|x|x|x|x| | | | | | | | | | | | | | | | | | --> TCSPC bin
# +-------------------------------+ +-------------------------------+
# +-------------------------------+ +-------------------------------+
# |x|x|x|x| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | --> Spectral/TCSPC input Channel
# +-------------------------------+ +-------------------------------+
WRAPAROUND = 65536 # After this sync counter will overflow
sync = np.bitwise_and(t3records, 65535) # Lowest 16 bits
tcspc = np.bitwise_and(np.right_shift(t3records, 16), 4095) # Next 12 bits, dtime can be obtained from header
chan = np.bitwise_and(np.right_shift(t3records, 28),15) # Next 4 bits
special = ((chan==15)*1)*(np.bitwise_and(tcspc,15)*1) # Last bit for special markers
index = ((chan==15)*1)*((np.bitwise_and(tcspc,15)==0)*1) # Find overflow locations
chan[chan==15] = special[chan==15]
chan[chan==1] = 0
chan[chan==2] = 1
chan[chan==4] = 0
elif record_type == 'rtPicoHarpT2':
print('TCSPC Hardware: {}'.format(record_type[2:]))
# +----------------------+ T2 32 bit record +---------------------+
# |x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x| |x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x| --> 32 bit record
# +-------------------------------+ +-------------------------------+
# +-------------------------------+ +-------------------------------+
# | | | | |x|x|x|x|x|x|x|x|x|x|x|x| |x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x| --> Sync
# +-------------------------------+ +-------------------------------+
# +-------------------------------+ +-------------------------------+
# |x|x|x|x| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | --> Spectral/TCSPC input Channel
# +-------------------------------+ +-------------------------------+
WRAPAROUND = 210698240 # After this sync counter will overflow
sync = np.bitwise_and(t3records, 268435455) # Lowest 28 bits
tcspc = np.bitwise_and(t3records, 15) # Next 4 bits, dtime can be obtained from header
chan = np.bitwise_and(np.right_shift(t3records, 28),15) # Next 4 bits
special = ((chan==15)*1)*np.bitwise_and(tcspc,15) # Last bit for special markers
index = ((chan==15)*1)*((np.bitwise_and(tcspc,15)==0)*1) # Find overflow locations
elif record_type in ['rtHydraHarpT3', 'rtHydraHarp2T3', 'rtTimeHarp260NT3', 'rtTimeHarp260PT3','rtMultiHarpNT3']:
print('TCSPC Hardware: {}'.format(record_type[2:]))
# +----------------------+ T3 32 bit record +---------------------+
# |x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x| |x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x| --> 32 bit record
# +-------------------------------+ +-------------------------------+
# +-------------------------------+ +-------------------------------+
# | | | | | | | | | | | | | | | | | | | | | | | |x|x|x|x|x|x|x|x|x|x| --> Sync
# +-------------------------------+ +-------------------------------+
# +-------------------------------+ +-------------------------------+
# | | | | | | | |x|x|x|x|x|x|x|x|x| |x|x|x|x|x|x| | | | | | | | | | | --> TCSPC bin
# +-------------------------------+ +-------------------------------+
# +-------------------------------+ +-------------------------------+
# | |x|x|x|x|x|x| | | | | | | | | | | | | | | | | | | | | | | | | | | --> Spectral/TCSPC input Channel
# +-------------------------------+ +-------------------------------+
# +-------------------------------+ +-------------------------------+
# |x| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | --> Special markers
# +-------------------------------+ +-------------------------------+
WRAPAROUND = 1024 # After this sync counter will overflow
sync = np.bitwise_and(t3records, 1023) # Lowest 10 bits
tcspc = np.bitwise_and(np.right_shift(t3records, 10), 32767) # Next 15 bits, dtime can be obtained from header
chan = np.bitwise_and(np.right_shift(t3records, 25),63) # Next 8 bits
special = np.bitwise_and(t3records,2147483648)>0 # Last bit for special markers
index = (special*1)*((chan==63)*1) # Find overflow locations
special = (special*1)*chan
elif record_type == 'rtHydraHarpT2':
print('TCSPC Hardware: {}'.format(record_type[2:]))
# +----------------------+ T3 32 bit record +---------------------+
# |x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x| |x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x| --> 32 bit record
# +-------------------------------+ +-------------------------------+
# +-------------------------------+ +-------------------------------+
# | | | | | | | |x|x|x|x|x|x|x|x|x| |x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x| --> Sync
# +-------------------------------+ +-------------------------------+
# +-------------------------------+ +-------------------------------+
# | |x|x|x|x|x|x| | | | | | | | | | | | | | | | | | | | | | | | | | | --> Spectral/TCSPC input Channel
# +-------------------------------+ +-------------------------------+
# +-------------------------------+ +-------------------------------+
# |x| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | --> Special markers
# +-------------------------------+ +-------------------------------+
WRAPAROUND = 33552000 # After this sync counter will overflow
sync = np.bitwise_and(t3records, 33554431) # Lowest 25 bits
chan = np.bitwise_and(np.right_shift(t3records, 25),63) # Next 6 bits
tcspc = np.bitwise_and(chan, 15)
special = np.bitwise_and(np.right_shift(t3records, 31),1) # Last bit for special markers
index = (special*1) * ((chan==63)*1) # Find overflow locations
special = (special*1)*chan
elif record_type in ['rtHydraHarp2T2', 'rtTimeHarp260NT2', 'rtTimeHarp260PT2','rtMultiHarpNT2']:
print('TCSPC Hardware: {}'.format(record_type[2:]))
# +----------------------+ T3 32 bit record +---------------------+
# |x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x| |x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x| --> 32 bit record
# +-------------------------------+ +-------------------------------+
# +-------------------------------+ +-------------------------------+
# | | | | | | | |x|x|x|x|x|x|x|x|x| |x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x| --> Sync
# +-------------------------------+ +-------------------------------+
# +-------------------------------+ +-------------------------------+
# | |x|x|x|x|x|x| | | | | | | | | | | | | | | | | | | | | | | | | | | --> Spectral/TCSPC input Channel
# +-------------------------------+ +-------------------------------+
# +-------------------------------+ +-------------------------------+
# |x| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | --> Special markers
# +-------------------------------+ +-------------------------------+
WRAPAROUND = 33554432 # After this sync counter will overflow
sync = np.bitwise_and(t3records, 33554431) # Lowest 25 bits
chan = np.bitwise_and(np.right_shift(t3records, 25),63) # Next 6 bits
tcspc = np.bitwise_and(chan, 15)
special = np.bitwise_and(np.right_shift(t3records, 31),1) # Last bit for special markers
index = (special*1) * ((chan==63)*1) # Find overflow locations
special = (special*1)*chan
else:
print('Illegal RecordType!')
exit(0)
# Fill in the correct sync values for overflow location
#sync[np.where(index == 1)] = 1 # assert values of sync = 1 just right after overflow to avoid any overflow-correction instability in next step
if record_type in ['rtHydraHarp2T3','rtTimeHarp260PT3','rtMultiHarpNT3']:
sync = sync + (WRAPAROUND*np.cumsum(index*sync)) # For HydraHarp V1 and TimeHarp260 V1 overflow corrections
else:
sync = sync + (WRAPAROUND*np.cumsum(index)) # correction for overflow to sync varibale
sync = np.delete(sync, np.where(index == 1), axis = 0)
tcspc = np.delete(tcspc, np.where(index == 1), axis = 0)
chan = np.delete(chan, np.where(index == 1), axis = 0)
special = np.delete(special, np.where(index == 1), axis = 0)
del index
# Convert to appropriate data type to save memory
self.sync = sync.astype(np.uint64, copy=False)
self.tcspc = tcspc.astype(np.uint16, copy=False)
self.channel = chan.astype(np.uint8, copy=False)
self.special = special.astype(np.uint8, copy=False)
print("Raw Data has been Read!\n")
return None
def get_flim_data_stack(self):
# Check if it's FLIM image
if self.head["Measurement_SubMode"] == 0:
raise IOError("This is not a FLIM PTU file.!!! \n")
sys.exit()
elif (self.head["ImgHdr_Ident"] == 1) or (self.head["ImgHdr_Ident"] == 5):
raise IOError("Piezo Scanner Data Reader Not Implemented Yet!!! \n")
sys.exit()
else:
# Create numpy array of important variables to be passed into numba accelaratd get_flim_data_stack_static function
# as numba doesn't recognizes python dict type files
# Check
header_variables = np.array([self.head["ImgHdr_Ident"], self.head["MeasDesc_Resolution"],self.head["MeasDesc_GlobalResolution"],self.head["ImgHdr_PixX"], self.head["ImgHdr_PixY"], self.head["ImgHdr_LineStart"],self.head["ImgHdr_LineStop"], self.head["ImgHdr_Frame"]],dtype = np.uint64)
sync = self.sync
tcspc = self.tcspc
channel = self.channel
special = self.special
del self.sync, self.tcspc, self.channel , self.special
flim_data_stack = get_flim_data_stack_static(sync, tcspc, channel, special, header_variables)
if flim_data_stack.ndim == 4:
tmp_intensity_image = np.sum(flim_data_stack, axis = 3) # sum across tcspc bin
intensity_image = np.sum(tmp_intensity_image, axis = 2)# sum across spectral channels
elif flim_data_stack.ndim == 3:
intensity_image = np.sum(flim_data_stack, axis = 3) # sum across tcspc bin, only 1 detection channel
return flim_data_stack, intensity_image
@njit
def get_lifetime_image(flim_data_stack,channel_number,timegating_start1,timegating_stop1,meas_resolution,estimated_irf):
work_data = flim_data_stack[:,:,channel_number,timegating_start1:timegating_stop1]
bin_range = np.reshape(np.linspace(0,timegating_stop1,timegating_stop1),(1,1,timegating_stop1))
fast_flim = (np.sum(work_data*bin_range,axis = 2)*meas_resolution)/np.sum(work_data,axis = 2)
return fast_flim