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()
Vapor 3.10.0
Python 3.9.19 (/opt/anaconda3/envs/vapor)
OpenGL 4.1 Metal - 89.3
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/e9bb9c5e8c05ae2ee38c35c371e36e8f55326a4ea1cba3d0dccf4efacafe73df.png

Changing Opacities

Vapor’s transferFunctionWidget allows you to adjust the opacity points of a renderer as you would within Vapor’s GUI.

from vapor.utils import histogram
histogram.transferFunctionWidget(ses, ren)

If you do not have access to the widget, we provide static options for changing the opacities as well. 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/905cd7f94d8a29c1d124031001db7cddcb935516ebf5ad1583aa0b57d042116f.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/6cf016623dad0352a2172ad5e27b083b2da3affc9ebdafb6000cfb819b440720.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/f02b6d0d178a21825be3cf08b0ff91839c1eb28a35fd45460f672f6974d1d4b9.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/371258180423b8c73e269632cd3960d7fc808da03423d8fbcd0feece715a8d07.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/2de348c7073f771bb68ecb6328ea505df7d469a014256820bd37d02814ba0539.png
Highlighting/oxy
../_images/dc099f5098b7c84a7f51f2931ee862ecf5bb082833eba9ea01ebd6521bb50576.png
Diverging/tarn
../_images/9bf3c864cab6974eb0d38b095a0c06d6c4b1098eb57545d38335ed18fe1390d3.png
Diverging/curl
../_images/6749599cd32621aaddbd63d7d97e3f2c68f1f06cc1f03be71104d993ec5bfe95.png
Diverging/CoolWarmSmooth
../_images/4c32b1e632d982d3e5d126eb2a9823ca7c514a2524f6c9f5169743c8fd2af46d.png
Diverging/PurpleWhiteOrange
../_images/87a2bf29e20f83da7a1a9e23c2c32ee78ae0f53ba7963c658160ff378111db7c.png
Diverging/diff
../_images/105d4ca01860cf8cac8e112819f8bb1770ac1076aed1c9d487e3e648545a3506.png
Diverging/GreenWhiteRed
../_images/1a407fd4720c230f42d5e9de26af3f178fae3bbc2601da840687137587cbec3b.png
Diverging/CoolWarmBent
../_images/0703c263c3ec3a938b90a4e7a3ac4c6557aa786a45ae148d028da8cc0e66a8c2.png
Diverging/BlueWhiteGold
../_images/eb3436af9d170d6730671c915750119314bdc1560dfa7a006f514a9c02b737a6.png
Diverging/CoolWarm
../_images/da5c80c9bca295f41a71550f48e2cf4295b057686bbb595af758be7918cff510.png
Diverging/GreenWhitePurple
../_images/5476f493bd335a5c622495e67712196d08d72b1daa72fb71ef8c4329b88a8241.png
Diverging/balance
../_images/fc8d317de82f5dd9fc43a9d0bdb1d8fc0625ace8fc6c7e88b30aff8311869a5c.png
Diverging/delta
../_images/15acac4a171e99d2031b44c14adeeaad91ff803d72300520325bee16c1af09ee.png
Sequential/BlackBody
../_images/bb2655677498206feb51151b6702f7de3181037f1d5e614dbb858f9995cd828e.png
Sequential/thermal
../_images/cd8523c549b6d04358ac216ab9f9624ae0cfdd54eb7b4c5de2e7fae39f8dd45a.png
Sequential/tempo
../_images/dd074c5197451b5d7b6e79861ec25bbe48715d64a49eb60e60181c618b770a58.png
Sequential/speed
../_images/5e96dc7196296e1502f58a8a0c3e682f9dba566704c66f19daf84773baae9eb8.png
Sequential/Kindlmann
../_images/48e208cf3a9215647b6fbea6335e21f648477eba5d4f982f6632001d5a243ae0.png
Sequential/KindlmannExtended
../_images/ab96b5dd630c1160408f85e565231f2ec5a2693da7de5dd7f8fa3fa5d7598ea3.png
Sequential/algae
../_images/1d1fac1750bee813c62165810db7f851132100d1f13fe46238a639c12d2668aa.png
Sequential/deep
../_images/e8ff726d8ed9ff38656d0cb46ba18123b192bdd6c09ef05cb3c5df39497bd39b.png
Sequential/haline
../_images/3de754c242d8150288cc01743f3589183c8fc29248a8b95e26722efa4bb7987e.png
Sequential/turbid
../_images/04cbb5e16f7e4ee60a83de7bb82042defd6bfa6542906fa1721bdbab40d2e836.png
Sequential/rain
../_images/e153d813e88eedb46c1ee55797939f54ed4e89daeb03b7d229c6db57e7b3f7eb.png
Sequential/Rainbow
../_images/45e125d7a4961f0cad6c388b83bea76b89284d3f01603135e7d98ff73208e2e1.png
Sequential/phase
../_images/5466cb2090e2ed0e81c8af12e7135a5bc575d24df50efbda15f3bff0638386ca.png
Sequential/BlackBodyExtended
../_images/52f208d0933198fb67905c36f1b6d0192bd164a90a18aa9962227bb885be6d8f.png
Sequential/dense
../_images/e3e918129f0daf8fb95a3a1dd04b9075e48466c77491f23f5a82d56b1bcf05f1.png
Sequential/amp
../_images/71343c4254a0420a6ec0cb0385e04e5a1b904236c90ca7ce6d402b74709156e1.png
Sequential/ice
../_images/5685411214fa4a81bf7bc984d92aa3a8705972a53ba2a49b9c8fc4789f0a6e9a.png
Sequential/matter
../_images/7ff1b067c7f1db2774bc8a7ee9e7cdbd2a53a18dcc8a1fd82563865be29d1611.png
Sequential/BlackWhite
../_images/21321d85c64534b088865128b247a8ccb5c95ccceb4273d80d8d2a7846a8ad70.png
Sequential/solar
../_images/84b4b029bd27be47d66d1015a47a96088e885504929b9ba011eb151e9c2c0f3a.png