<jupyter_start><jupyter_text>How to Decode T3R RecordsThis Notebook shows how to decode raw T3R records.Author(s): *<NAME>*  Required Packages<jupyter_code>import numpy as np
pi = np.pi
import matplotlib.pyplot as plt
%matplotlib inline

import importlib
import pqreader.pqreader as pq # import package from local directory
importlib.reload(pq)
plt.rcParams.update({'font.size' : 14,
                     'axes.labelpad' : 10,
                     'xtick.major.pad': 8,
                     'xtick.major.size': 6,
                     'xtick.major.width': 1,
                     'ytick.major.size': 6,
                     'ytick.major.width': 1,
                     'xtick.minor.size': 3,
                     'xtick.minor.width': 1,
                     'ytick.minor.size': 3,
                     'ytick.minor.width': 1,
                     'axes.linewidth': 1,
                     'xtick.top' : True,
                     'xtick.direction' : 'in',
                     'ytick.right' : True,
                     'ytick.direction' : 'in',
                     'lines.linewidth' : 1.5,})
t3records, _ = pq.t3r_records('Data/Nile_Blue_Glycerol.t3r')
t3records[0] 
np.binary_repr(t3records[0], 32)
valid_bits = 1
route_bits = 2
data_bits = 12
timetag_bits = 16
np.bitwise_and(np.right_shift(t3records[0], timetag_bits + data_bits + route_bits), 2**valid_bits - 1)
np.binary_repr(np.bitwise_and(np.right_shift(t3records[0], 16), 2**data_bits-1), data_bits)
np.binary_repr(np.bitwise_and(t3records[0], 2**timetag_bits-1), timetag_bits)
valid = np.bitwise_and(np.right_shift(t3records, timetag_bits + data_bits + route_bits), 2**valid_bits - 1).astype('uint8')
route = np.bitwise_and(np.right_shift(t3records, timetag_bits + data_bits), 2**route_bits - 1).astype('uint8')
data  = np.bitwise_and(np.right_shift(t3records, timetag_bits), 2**data_bits - 1).astype('uint16')
timetags = np.bitwise_and(t3records, 2**timetag_bits - 1).astype('uint64')
fig, ax = plt.subplots(1, 1, figsize=(7,5))
ax.plot(route[:100], 'ko')
fig, ax = plt.subplots(1, 1, figsize=(7,5))
ax.plot(timetags[:30000], 'ko')
def correct_overflow(timetags, valid, overflow):
    overflow_idx = np.where(valid==0)[0]
    for i, (idx1, idx2) in enumerate(zip(overflow_idx[:-1], overflow_idx[1:])):
        timetags[idx1:idx2] += (i + 1)*overflow
    timetags[idx2:] += (i + 2)*overflow
    return np.delete(timetags, np.where(valid==0)[0])
overflow = 2**timetag_bits
timetags = correct_overflow(timetags, valid, overflow)
fig, ax = plt.subplots(1, 1, figsize=(7,5))
ax.plot(timetags[14510870:], 'ko')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup