Transfer Functions

You can change a visualization’s appearance by accessing and modifying its transfer function.

import example_utils
from vapor import session, renderer, dataset, camera, transferfunction
from vapor.utils import histogram

ses = session.Session()
data = example_utils.OpenExampleDataset(ses)

ren = data.NewRenderer(renderer.VolumeRenderer)
ses.GetCamera().LookAt((32, 120, 120), (32, 32, 32))
ses.Show()
Warning: sysroot "/Applications/Xcode_12.4.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk" not found (ignoring for now).
Vapor 3.9.3
Python 3.9.19 (/opt/anaconda3/envs/vapor)
OpenGL 4.1 Metal - 83.1
UNSUPPORTED (log once): POSSIBLE ISSUE: unit 3 GLD_TEXTURE_INDEX_3D is unloadable and bound to sampler type (Float) - using zero texture because texture unloadable
../_images/e0612cb8ca7a0ecba2e349f8b3d281f77ef161e55f9415c5776077c5a5b7e960.png

Changing Opacities

We created a volume rendering however it is fully opaque. We can use a transfer function to adjust the visible portions. Before we adjust the opacity map of the TF, we get a histogram to help us determine what we want to hide.

histogram.ShowMatPlotLibHistogram(ses, ren)
../_images/9996235b994e2413a963f3c5dcaf7e815ee7665dcad94acc7fc770b3e82d3bf4.png

Usually we want to hide the most common value so below we construct an opacity map that accomplishes this.

# List of x,y pairs where x is the data value and y is the opacity for that data value
opacities = [(-0.3, 1), (-0.1, 0), (0.1, 0), (0.3, 1)]

We can get the matplotlib histogram plot and add our opacity map to it to compare.

plt = histogram.GetMatPlotLibHistogram(ses, ren)
plt.plot(*zip(*opacities))
plt.show()
../_images/b7191fd7c490c8f5163acf3c5c1a2f5e8df20510f3cf6cbe80ce63eb3c64b612.png

Now we apply the map to the transfer function

# Renderers can have multiple transfer functions.
# GetPrimaryTransferFunction returns the one that is usually the most useful.
# You can use `tf.GetTransferFunction(var_name)` to get other transfer functions.
tf = ren.GetPrimaryTransferFunction()
tf.SetOpacityControlPoints(opacities)
ses.Show()
../_images/a99e7785df071fdca34f57b23ada7010809ad4a6cb9f89e8da31b32e5452f654.png

You can adjust the colormap in a similar fashion. Use help(tf) for more information. Vapor includes a list of built-in colormaps and these can be applied with tf.LoadBuiltinColormap(name)

Builtin Colormaps

tf.LoadBuiltinColormap("Sequential/BlackBodyExtended")
ses.Show()
../_images/9e0aad32a7f67d201b97538801082c6021185e88a5f12b8e143ef3370b1bb416.png

List of All Builtin Colormaps

ses.DeleteRenderer(ren)
ren = data.NewRenderer(renderer.TwoDDataRenderer)
tf = ren.GetPrimaryTransferFunction()

for cmap in transferfunction.TransferFunction.ListBuiltinColormaps():
    tf.LoadBuiltinColormap(cmap)

    print(cmap)
    tf.ShowMatPlotLibColorbar()
Highlighting/topo
../_images/6b974b7eefe2bb929945df70bcf142b70b163c08651a14b626539aca86be502b.png
Highlighting/oxy
../_images/0185659624893a6c60432010b08fcad95991266fdbdfa3fc7ebf54abeb7ab35f.png
Diverging/tarn
../_images/f063f27e0160fcaf6706ed7f0e05a5399db1556a0c04205303b16ccba6e3ac1d.png
Diverging/curl
../_images/a253968331f1494c900605ce41c4559341d0ec9340db7f50d244d710ca09aa84.png
Diverging/CoolWarmSmooth
../_images/404576a499c96426d82008be790950acd410bd71be5258241b48fc0a1073f3f5.png
Diverging/PurpleWhiteOrange
../_images/d593c54c9af6f18410f56c56d270e3afda7482479b5ebd49772bfd716f8a237f.png
Diverging/diff
../_images/cf52bc63e268a3505a070729c05d7d12e5ca563464fc7599bf12b7d2a2e4fce3.png
Diverging/GreenWhiteRed
../_images/d1233391a458b9ef573fef05693ddfa93cd4363e0393213347827bd66ec78663.png
Diverging/CoolWarmBent
../_images/da7b075b2491daea08671d513df2e2ead11846c6b954f08d606e9dfe24f44459.png
Diverging/BlueWhiteGold
../_images/8940709aa905b893b83fb8e7adbefe62271f0736913bb9550f88b20d7596a813.png
Diverging/CoolWarm
../_images/f496790457f502ee84c3dea8b4c1231d2a8d5df553045df38972ceedb4287dfb.png
Diverging/GreenWhitePurple
../_images/6161270fd700a5920690a64fc4bb81f2959356d62172fa6c662b3383d5d6163a.png
Diverging/balance
../_images/d5866d369466ce27ce73b51966cb0e3731102d573faada7e6dccae1aba46a80d.png
Diverging/delta
../_images/7c61f042a38b06054c49f3e0ee4ac7dde823916fc9df270146682eb31fef5209.png
Sequential/BlackBody
../_images/15b6d0f95d7a31f1cd966b17dd068ad960e72a4e1feb57a161aed19392cd6690.png
Sequential/thermal
../_images/3137abe91fb7140eb48d0bfc837557b9eb69285a669a614802dd803f932882d8.png
Sequential/tempo
../_images/484ee4f8cafd4dca49b955c2564360ab508815b9c72f97d3062b97617bd31b5d.png
Sequential/speed
../_images/1cdd77086e340fc2ee3ab0097e918a21f1b6750eb4d8649bf1641b923761e2e8.png
Sequential/Kindlmann
../_images/2328f1878b7006757ecce8a2f3f109d4f3c91897ae14f390dd6011e7e0d34ff6.png
Sequential/KindlmannExtended
../_images/f1f58f4ad28d90c3d0f50f51fb48bd47b6f92c01fd40733f4a1abd5ec8e3f928.png
Sequential/algae
../_images/e568e35fa14bf565795710fbbd4495c0efec73cd5891d385604199c986c85876.png
Sequential/deep
../_images/d12004b3f2e89283ae9f21a302711bd92c4d20b5c9ab549ad702f6b094f1761c.png
Sequential/haline
../_images/7869fd55b46c668c0861d930238ac46f7ad55053830c3f52347b7387b6802a37.png
Sequential/turbid
../_images/c0c2860b0168d387fdb9650ff59f795a839a8d33ddf8a9afeef612f168edcf35.png
Sequential/rain
../_images/10751c42c7ef1399ae2767e95482197a7a9f5f6c77116275b0e9c092f54e330b.png
Sequential/Rainbow
../_images/4fbdae4a11ed3a6309a99520d556ef76dcb7d411b09abfda79cb49d632f4684f.png
Sequential/phase
../_images/ec47d34e74c2812bb1b0a29b4dfc1bb9341c960214f54ec9e8c7ccdc19b475d0.png
Sequential/BlackBodyExtended
../_images/2be121a98d68a32757e9815b235f24694518f079538074bff4f09161520320c4.png
Sequential/dense
../_images/60da62d72e02350ce586ace93807801917526caffdff7ccb9a7bfab9a8cd9dd1.png
Sequential/amp
../_images/c0fe58598ba82e38a00a03159b7b445e180087e82ee7d49c1e9a6249327dc2ca.png
Sequential/ice
../_images/7d9d584287392e88ecb18c9aeabdb4993c8051413b125159fe264e67a357e3d5.png
Sequential/matter
../_images/4bdfbe5aef5b19258e743865c9544ddd8c2535db0391495f73d88b62baf367d0.png
Sequential/BlackWhite
../_images/e760eb99826e781338474c1870cb287a8398d61acd2126ce2afd91165925309d.png
Sequential/solar
../_images/866a60d9bede2f44818a917f4fce41e7c482ecd5ca638ae47940a6dc582eba0f.png