<jupyter_start><jupyter_text>![](tck2.png)<jupyter_code>USERNAME = 'haehn'
DATA = '/home/'+USERNAME+'/Dropbox/TRAKODATA/ADHD_STFC_diff_for_Daniel_TRK/101/tracts_left_hemisphere/'<jupyter_output><empty_output><jupyter_text>Nibabel<jupyter_code>nib.streamlines.FORMATS
trk = os.path.join(DATA,'cluster_00001.trk')
trk2 = os.path.join(DATA,'cluster_00002.trk')
loaded_trk = nib.streamlines.load(trk, lazy_load=False)
loaded_trk.save('/tmp/test1.tck')
tck = nib.streamlines.TckFile(loaded_trk.tractogram)
tck.save('/tmp/test2.tck')<jupyter_output>/home/d/miniconda3/envs/TRAKODEV/lib/python3.6/site-packages/nibabel/streamlines/tck.py:218: DataWarning: TCK format does not support saving additional data alongside streamlines. Dropping: EmbeddingCoordinate, ClusterNumber, EmbeddingColor, TotalFiberSimilarity, MeasuredFiberSimilar
  warnings.warn(msg, DataWarning)
/home/d/miniconda3/envs/TRAKODEV/lib/python3.6/site-packages/nibabel/streamlines/tck.py:225: DataWarning: TCK format does not support saving additional data alongside points. Dropping: NormalizedSignalEsti, EstimatedUncertainty, RTOP1, RTOP2, RTAP1, RTAP2, RTPP1, RTPP2, SignalMean
  warnings.warn(msg, DataWarning)<jupyter_text>![](tck1.png)  Dipy<jupyter_code>dipy.io.streamline.load_tractogram(trk, 'same')<jupyter_output>/home/d/miniconda3/envs/TRAKODEV/lib/python3.6/site-packages/nibabel/streamlines/trk.py:629: HeaderWarning: Voxel order is not specified, will assume 'LPS' since it is Trackvis software's default.
  warnings.warn(msg, HeaderWarning)
WARNING:root:Voxel order must be string/char.
WARNING:root:Voxel order does not follow convention.
WARNING:root:Voxel order must be string/char.
WARNING:root:Voxel order does not follow convention.
WARNING:root:Voxel order must be string/char.
WARNING:root:Voxel order does not follow convention.
WARNING:root:Voxel order must be string/char.
WARNING:root:Voxel order does not follow convention.
WARNING:root:Voxel order must be string/char.
WARNING:root:Voxel order does not follow convention.
WARNING:root:Voxel order must be string/char.
WARNING:root:Voxel order does not follow convention.
WARNING:root:Voxel order must be string/char.
WARNING:root:Voxel order does not follow convention.
WARNING:root:Voxel order must be string/char.
WARNING:root:Voxel order doe[...]<jupyter_text>Nibabel 2nd TRK<jupyter_code>loaded_trk = nib.streamlines.load(trk2, lazy_load=False)
tck = nib.streamlines.TckFile(loaded_trk.tractogram)
tck.save('/tmp/test3.tck')<jupyter_output><empty_output><jupyter_text>![](tck3.png)  Let's test many TRK files<jupyter_code>all_clusters = os.listdir(DATA)
for c in all_clusters:
    trk = os.path.join(DATA, c)
    loaded_trk = nib.streamlines.load(trk, lazy_load=False)
    tck = nib.streamlines.TckFile(loaded_trk.tractogram)
    out_tck = '/tmp/'+c.replace('trk','tck')
    tck.save(out_tck)
    
    ret = os.system('/home/d/Projects/qfib/qfib '+out_tck+' '+out_tck.replace('tck', 'qfib'))
    if ret == 0:
        print('Compressed', out_tck)<jupyter_output>/home/haehn/miniconda3/envs/TRAKODEV/lib/python3.6/site-packages/nibabel/streamlines/trk.py:629: HeaderWarning: Voxel order is not specified, will assume 'LPS' since it is Trackvis software's default.
  warnings.warn(msg, HeaderWarning)
/home/haehn/miniconda3/envs/TRAKODEV/lib/python3.6/site-packages/nibabel/streamlines/tck.py:218: DataWarning: TCK format does not support saving additional data alongside streamlines. Dropping: EmbeddingCoordinate, ClusterNumber, EmbeddingColor, TotalFiberSimilarity, MeasuredFiberSimilar
  warnings.warn(msg, DataWarning)
/home/haehn/miniconda3/envs/TRAKODEV/lib/python3.6/site-packages/nibabel/streamlines/tck.py:225: DataWarning: TCK format does not support saving additional data alongside points. Dropping: NormalizedSignalEsti, EstimatedUncertainty, RTOP1, RTOP2, RTAP1, RTAP2, RTPP1, RTPP2, SignalMean
  warnings.warn(msg, DataWarning)<jupyter_text>![](tck4.png) ![](tck5.png) ![](tck6.png) ![](tck7.png)  TRAKO!<jupyter_code>loaded_trk = nib.streamlines.load(os.path.join(DATA,'cluster_00788.trk'), lazy_load=False)
len(loaded_trk.streamlines)
loaded_trk.streamlines[0].ravel()
fiberlengths = []
streamlines = np.array([], dtype=np.float32)
for s in loaded_trk.streamlines:
    fiberlengths.append(len(s))
    streamlines = np.append(streamlines, s.ravel())
    
c_fiberlengths = TrakoDracoPy.encode_point_cloud_to_buffer(fiberlengths, position=False, sequential=True, 
        quantization_bits=14, compression_level=1, quantization_range=-1, quantization_origin=None)

for qb in range(1,15):

    position = True
    sequential = True
    # qb=10 #quantization bits
    cl=1 #compression level
    qrange=-1
    qorigin=None
    c_streamlines = TrakoDracoPy.encode_point_cloud_to_buffer(streamlines, position=position, sequential=sequential, 
              quantization_bits=qb, compression_level=cl, quantization_range=qrange, quantization_origin=qorigin)
    print('='*80)
    print('QB', qb)
    print('Bare minimum Trako Bytesize', len(c_fiberlengths+c_streamlines))

    # now decode again
    r_streamlines = TrakoDracoPy.decode_point_cloud_buffer(c_streamlines).points

    stats = TKO.Util.error(streamlines, r_streamlines)[0]
    print('Max error', stats[1])
    print('Mean error', stats[2])
    print('Min error', stats[0])
    print('Compression Ratio', round((1-len(c_fiberlengths+c_streamlines)/float(streamlines.nbytes))*100,4), '%')
    print('Compression Factor', str(round(float(streamlines.nbytes) / len(c_fiberlengths+c_streamlines),1))+'x')
restored_tck = nib.streamlines.load('/tmp/cluster_00788.restored.tck', lazy_load=False)
c_streamlines = restored_tck.streamlines.get_data().ravel()
stats = TKO.Util.error(streamlines, c_streamlines)[0]
print('Max error', stats[1])
print('Mean error', stats[2])
print('Min error', stats[0])
streamlines[0:10]
restored_tck.streamlines.get_data().ravel()[0:10]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12584688346883469, 'fraction_numerical': 0.02049457994579946, 'mean_word_length': 6.418341708542713, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '255233', 'license': 'MIT', 'n_tokens_mistral': 2241, 'n_tokens_neox': 2135, 'n_words': 364, 'path': 'IPY/TCK_TRK.ipynb', 'repo_name': 'bostongfx/TRAKO', 'size': '5904'}