Skip to content

Commit

Permalink
Merge pull request #3 from neurolabusc/main
Browse files Browse the repository at this point in the history
Lookup table to improve float16 performance
  • Loading branch information
arokem authored Jan 17, 2024
2 parents d385ced + 8ffbcac commit 29d00a4
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 33 deletions.
Binary file removed M1.png
Binary file not shown.
Binary file added M2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ This code should be supported by all modern web browsers. If you want to use thi

The module `streamlineIO.mjs` provides JavaScript reading for streamlines in the TCX, TRK, TRX, and VTK formats. The included file `bench.mjs` demonstrates these capabilities by loading a streamline 11 times and reporting details regarding file size, loading time, and streamline properties.

Assuming you have [node 14.11.2 or later]([web site](https://nodejs.org/en/download/) you can run the demo:
Assuming you have [node 14.11.2 or later](https://nodejs.org/en/download/) you can run the demo:

```
$ git clone https://github.com/tee-ar-ex/trx-javascript
Expand Down Expand Up @@ -68,7 +68,7 @@ There are several important considerations regarding supporting the TRX format w

The included JavaScript `bench` provides a method to evaluate performance. This benchmark is likely specific to JavaScript and so caution should be excercised in evaluating relative performance. The script will report the time to load a TRK, TCK, VTK or TRX file 10 times (it loads the tracts 11 times, and ignores the first run).

The graph below shows the time load the [left inferior fronto-occipital fasciculus (IFOF) ](https://brain.labsolver.org/hcp_trk_atlas.html) with 30856 streamlines and 11437105 vertices. The benchmark was run on a passively-cooled 15w M1-based MacBook Air. The ideal format would be both fast to load (to the left on the horizontal axis) and have a small file size (toward the bottom in the right axis). However, compression typically trades load time for file size. Here all data is loaded from a local solid state drive, whereas smaller files would benefit if data was loaded using a slow internet connection. The following file formats are illustrated (except where noted, both positions and indices are stored with 32-bit precision):
The graph below shows the time load the [left inferior fronto-occipital fasciculus (IFOF) ](https://brain.labsolver.org/hcp_trk_atlas.html) from the HCP1065 Population-Averaged Tractography Atlas (Yeh, 2022). This has with 21209 streamlines and 4915166 vertices. The different formats are generated with the [tff_convert_tractogram.py](https://github.com/tee-ar-ex/trx-python) script.The benchmark was run on a MacBook laptop with a M2 Pro CPU. The ideal format would be both fast to load (to the left on the horizontal axis) and have a small file size (toward the bottom in the right axis). However, compression typically trades load time for file size. Here all data is loaded from a local solid state drive, whereas smaller files would benefit if data was loaded using a slow internet connection. The following file formats are illustrated (except where noted, both positions and indices are stored with 32-bit precision):

- vtk: streamlines saved by an [undocumented](https://discourse.vtk.org/t/upcoming-changes-to-vtkcellarray/2066) extension to the [VTK legacy file format](https://vtk.org/wp-content/uploads/2015/04/file-formats.pdf).
- tck: [MRtrix format](https://mrtrix.readthedocs.io/en/latest/getting_started/image_data.html#tracks-file-format-tck).
Expand All @@ -80,4 +80,4 @@ The graph below shows the time load the [left inferior fronto-occipital fascicul
- 16.trx: uncompressed TRX format using 16-bit floats for positions (which are not native to JavaScript).
- 16z.trx: zip-compressed TRX format using 16-bit floats for positions (which are not native to JavaScript).

![M1 Performance](M1.png)
![M2 Performance](M2.png)
29 changes: 0 additions & 29 deletions graphM1.py

This file was deleted.

32 changes: 32 additions & 0 deletions graphM2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# Create a dataframe
data = {
'format': ['vtk', 'tck', 'trk', 'trk.gz', 'trk.zst', 'trx', 'z.trx', '16.trx', '16z.trx'],
'time': [459, 866, 924, 3881, 4012, 159, 2763, 373, 2708],
'bytes': [113218694, 59236654, 59067828, 28746432, 28291562, 59152204, 28502799, 29661208, 24245538]
}

df = pd.DataFrame(data)

# Define a color palette with as many colors as there are formats
colors = sns.color_palette('husl', n_colors=len(df['format']))

# Set a larger figure size
plt.figure(figsize=(10, 5))

# Create a scatter plot with different colors for each format and double the diameter of each point
ax = sns.scatterplot(x='time', y='bytes', hue='format', palette=colors, s=200, data=df)

# Set the legend outside the plot for better visibility
ax.legend(title='Format', bbox_to_anchor=(1.05, 1), loc='upper left')

plt.title('Relative Time vs Size for Each Format')
plt.xlabel('Relative Time')
plt.ylabel('Size (Bytes)')

plt.tight_layout() # Adjust layout to prevent clipping
#plt.show()
plt.savefig('M2.png', dpi=300)
4 changes: 3 additions & 1 deletion streamlineIO.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,9 @@ async function readTRX(url, urlIsLocalFile = false) {
nval = data.length / 2; //2 bytes per 16bit input
vals = new Float32Array(nval);
var u16 = new Uint16Array(data.buffer);
for (let i = 0; i < nval; i++) vals[i] = decodeFloat16(u16[i]);
const lut = new Float32Array(65536)
for (let i = 0; i < 65536; i++) lut[i] = decodeFloat16(i)
for (let i = 0; i < nval; i++) vals[i] = lut[u16[i]]
} else continue; //not a data array
nval = vals.length;
//next: read data_per_group
Expand Down

0 comments on commit 29d00a4

Please sign in to comment.