def binFile2Data(fname, storePickle=False): """ Read a binary FCS data file, containing a list of U64 numbers that represent the photon counts per microtime bin of about 1 µs. ========== =============================================================== Input Meaning ---------- --------------------------------------------------------------- fname Binary file name ========== =============================================================== ========== =============================================================== Output Meaning ---------- --------------------------------------------------------------- countArray N x 26 array with for each row the number of photon counts for each channel and the sum of all channels per microbin ========== =============================================================== """ from readBinFile import readBinFile from u64ToCounts import u64ToCounts import numpy as np import os from savevar import savevar readLength = 8 * 65536 # Get file size [in bytes] fsize = os.path.getsize(fname) # Empty array to store photon counts countArray = np.zeros((int(fsize / 2 / 8), 26), dtype='H') startPos = 0 # position in the .bin file to start reading row = 0 # row of countArray to write to # Go through the file, read a chunk of data, write to the array, and repeat while startPos < fsize: print(startPos / fsize * 100) # Read part of the binary file data = readBinFile(fname, startPos, readLength) startPos += readLength # Calculate photon counts for i in range(len(data)): counts = u64ToCounts(data[i]) # add counts to array countArray[int(np.floor(row / 2))][:] = np.add( countArray[int(np.floor(row / 2))][:], counts) row += 1 print(100) if storePickle: print("Storing .pickle file") savevar(countArray, fname[0:-4] + "_data") print(".pickle file stored") return countArray
def f(fname, readLength, clustNumb): # Calculate photon counts in a chunck of data clustNumb = int(clustNumb) row = 0 startPos = clustNumb * readLength data = readBinFile(fname, startPos, readLength) countArrayClust = np.zeros((int(len(data) / 2), 26), dtype='H') for i in range(len(data)): counts = u64ToCounts(data[i]) # add counts to array countArrayClust[int(np.floor(row / 2))][:] = np.add( countArrayClust[int(np.floor(row / 2))][:], counts) row += 1 return countArrayClust
def getCountsFromDataLine(rawdata, i): # import functions from u64ToCounts import u64ToCounts start = 8 * i # number of bytes wrt the start of the file where number i is stored newRawDataPoint = rawdata[start:start+8] # Each U64 consists of 8 bytes newDataPoint = newRawDataPoint[0] # byte 0 # bytes 1-7: for j in range(7): newDataPoint = (newDataPoint << 8) + newRawDataPoint[j+1] counts = u64ToCounts(newDataPoint) return counts
def analyzeFCSData(fname): countArray = [] counts = [0] * 25 with open(fname) as f: lineNr = 0 for line_terminated in f: if lineNr % 1000000 == 0: print(lineNr) line = int(line_terminated.rstrip('\n')) count = u64ToCounts(line) if sum(count) > 0: for i in range(len(count)): detElCounts = count[i] for j in range(detElCounts): arrivalTime = math.floor(lineNr / 2) detEl = i p = Photon(arrivalTime, detEl) countArray.append(p) counts = list(map(add, counts, count)) lineNr += 1 return counts, countArray
def binFile2ArrivalTimes(fname, storePickle=False, dwellTime=[]): """ Read a binary FCS data file, containing a list of U64 numbers that represent the photon counts per microtime bin of about 1 µs and store the arrival times in units of dwellTime. Storing arrivaltimes in general reduces the file size significantly =========================================================================== Input Meaning --------------------------------------------------------------------------- fname Binary file name storePickle Store result in Pickle file dwellTime Pixel dwell time [s] =========================================================================== =========================================================================== Output Meaning --------------------------------------------------------------------------- arrivalTimes 26 lists with the arrival times of the photon for each of the 25 detector elements + the sum =========================================================================== """ readLength = 8 * 65536 # Get file size [in bytes] fsize = os.path.getsize(fname) # Empty object to store photon arrival times arrivalTimes = ArrivalTimesObject() for i in range(25): setattr(arrivalTimes, 'det' + str(i), []) arrivalTimes.sum = [] arrivalTimes.dwellTime = dwellTime startPos = 0 # position in the .bin file to start reading t = 0 # iterator used to indicate current time bin # Go through the file, read a chunk of data, write to the object, and repeat while startPos < fsize: print(startPos / fsize * 100) # Read part of the binary file data = readBinFile(fname, startPos, readLength) startPos += readLength # Calculate photon counts for i in range(len(data)): counts = u64ToCounts(data[i]) # go through each detector element for det in range(25): for c in range(counts[det]): getattr(arrivalTimes, 'det' + str(det)).append(int(np.floor(t / 2))) # sum of all detector elements for c in range(counts[25]): arrivalTimes.sum.append(int(np.floor(t / 2))) t += 1 print(100) if storePickle: print("Storing .pickle file") savevar(arrivalTimes, fname[0:-4] + "_arrival_times.pickle") print(".pickle file stored") return arrivalTimes
def binFile2Image(fname, storePickle=False): """ Read a binary image data file, containing a list of U64 numbers that represent the photon counts per microtime bin of about 1 µs per pixel. ========== =============================================================== Input Meaning ---------- --------------------------------------------------------------- fname Binary file name ========== =============================================================== ========== =============================================================== Output Meaning ---------- --------------------------------------------------------------- countArray N x 26 array with for each row the number of photon counts for each channel and the sum of all channels per microbin ========== =============================================================== """ from readBinFile import readBinFile from u64ToCounts import u64ToCounts import os readLength = 8 * 65536 # Image dimensions Nf = 1 # number of frames Nl = 500 # number of lines Np = 500 # number of pixels per line Nt = 10 # number of time bins per pixel Nd = 25 # number of detector element rows # Get file size [in bytes] fsize = os.path.getsize(fname) # Empty array to store photon counts countArray = np.zeros((Nf, Nl, Np, Nt, Nd + 1), dtype='H') startPos = 0 # position in the .bin file to start reading frame = 0 # row of countArray to write to scany = 0 scanx = 0 timebin = 0 cluster = 0 # Go through the file, read a chunk of data, write to the array, and repeat while startPos < fsize: print(startPos / fsize * 100) # Read part of the binary file data = readBinFile(fname, startPos, readLength) startPos += readLength # Calculate photon counts for i in range(len(data)): counts = u64ToCounts(data[i]) # add counts to array # sum two clusters clusterSum = np.add(countArray[frame][scany][scanx][timebin][:], counts) countArray[frame][scany][scanx][timebin][:] = clusterSum cluster += 1 if cluster == 2: cluster = 0 timebin += 1 if timebin == Nt: timebin = 0 scanx += 1 if scanx == Np: scanx = 0 scany += 1 if scany == Nl: scany = 0 frame += 1 print(100) if storePickle: print("Storing .pickle file") savevar(countArray, fname[0:-4] + "_data") print(".pickle file stored") return countArray