Finance利用 高斯溅射和 等值面提取技术可视化金融数据

一:主要的知识点

1、说明

本文只是教程内容的一小段,因博客字数限制,故进行拆分。主教程链接:vtk教程------逐行解析官网所有Python示例-CSDN博客

2、知识点纪要

本段代码主要涉及的有①高斯泼溅的意义及用法

二:代码及注释

python 复制代码
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonCore import vtkPoints, vtkFloatArray
from vtkmodules.vtkCommonDataModel import vtkUnstructuredGrid
from vtkmodules.vtkImagingHybrid import vtkGaussianSplatter
from vtkmodules.vtkFiltersCore import vtkContourFilter, vtkTubeFilter
from vtkmodules.vtkFiltersGeneral import vtkAxes
from vtkmodules.vtkRenderingCore import (
    vtkActor,
    vtkPolyDataMapper,
    vtkRenderWindow,
    vtkRenderWindowInteractor,
    vtkRenderer
)


def read_file(filename):
    res = {}

    with open(filename) as ifn:
        k = ''
        v = list()
        for line in ifn:
            cl = ' '.join(line.split()).split()
            if cl:
                if len(cl) == 2 and cl[0] == 'NUMBER_POINTS':
                    k = cl[0]
                    v = [int(cl[1])]
                    has_key = True
                    continue
                if len(cl) == 1 and not has_key:
                    has_key = True
                    k = cl[0]
                    v = list()
                else:
                    v += map(float, cl)
            else:
                if has_key:
                    # Normalise the data.
                    minimum = min(v)
                    maximum = max(v)
                    # Emulate the bug in the C++ code.
                    for i in v:
                        if i > minimum:
                            maximum = i
                    if maximum != minimum:
                        res[k] = list(map(lambda x: minimum + x / (maximum - minimum), v))
                    else:
                        res[k] = v
                    has_key = False
    return res


def make_dataset(filename, keys):
    res = read_file(filename)
    if res:
        newPts = vtkPoints()
        newScalars = vtkFloatArray()
        xyz = list(zip(res[keys[1]], res[keys[2]], res[keys[3]]))  # shape = 3188 * 3
        for i in range(0, res[keys[0]][0]):
            newPts.InsertPoint(i, xyz[i])
            newScalars.InsertValue(i, res[keys[4]][i])

        dataset = vtkUnstructuredGrid()
        dataset.SetPoints(newPts)
        dataset.GetPointData().SetScalars(newScalars)
        return dataset


def main():
    colors = vtkNamedColors()
    colors.SetColor("PopColor", [230, 230, 230, 255])

    fileName = "Data/financial.txt"
    keys = ['NUMBER_POINTS', 'MONTHLY_PAYMENT', 'INTEREST_RATE', 'LOAN_AMOUNT', 'TIME_LATE']

    dataSet = make_dataset(fileName, keys)

    """
    高斯泼溅 用于将离散的点云数据转换成连续的体数据(或图像)
    工作原理:
    想象一下你有一张纸,上面随机散落着一些点。你想用这些点来描绘出一片连续的区域密度。高斯溅射就是这么做的:
    高斯核(Gaussian Kernel): 想象在每一个输入数据点的位置,你放置了一个高斯钟形曲线(类似于正态分布的形状),这就是高斯核。
    溅射(Splatting): 这个高斯核就像一个"墨滴",它将自己的**值(密度或权重)**向周围区域扩散。离点越近,贡献的密度值越高;离点越远,贡献的值越低。
    累积求和: 在你设定的 3D 网格(或 2D 图像)中的每个位置(像素或体素),所有输入点的高斯核贡献的密度值都会被累加起来。
    最终输出:
    最终的输出是一个新的数据集(通常是 vtkImageData),它的值代表了原始点云在空间中的局部密度。数据点密集的地方,输出值就高;数据点稀疏的地方,输出值就低
    """
    popSplatter = vtkGaussianSplatter()
    popSplatter.SetInputData(dataSet)
    popSplatter.SetSampleDimensions(100, 100, 100)
    # SetRadius 设置高斯核半径
    popSplatter.SetRadius(.05)
    """
    ScalarWarpingOff/On  标量变形与否
    输入点的标量值(例如,颜色或温度)会用来缩放高斯核的高度。
    这使得高斯溅射不仅反映点的位置密度,还反映标量值的总和或平均分布
    """
    popSplatter.ScalarWarpingOff()

    popSurface = vtkContourFilter()
    popSurface.SetInputConnection(popSplatter.GetOutputPort())
    popSurface.SetValue(0, 0.01)

    popMapper = vtkPolyDataMapper()
    popMapper.SetInputConnection(popSurface.GetOutputPort())
    popMapper.ScalarVisibilityOff()

    popActor = vtkActor()
    popActor.SetMapper(popMapper)
    popActor.GetProperty().SetOpacity(0.3)
    popActor.GetProperty().SetColor(colors.GetColor3d('PopColor'))
    popSplatter.Update()

    lateSplatter = vtkGaussianSplatter()
    lateSplatter.SetInputData(dataSet)
    lateSplatter.SetSampleDimensions(50, 50, 50)
    lateSplatter.SetRadius(0.05)
    """
    SetScaleFactor 控制输出密度场的整体强度或大小
    当你调用 SetScaleFactor(value) 时,VTK 会执行以下操作:

    最终密度=高斯核贡献的总和×value
    如果 value > 1.0: 输出密度场的值会整体增大。这会使你提取等值面(Contour)时,更容易在较低的等值面值上获得一个更大的曲面。
    如果 value < 1.0: 输出密度场的值会整体减小。这使得密度场更"稀疏",你需要提取一个更低的值才能获得一个可见的曲面。
    逾期贷款只是总人口中的一个子集,点的数量要少得多。如果使用默认的 1.0 缩放因子,它们生成的密度场可能非常弱,难以提取出有意义的等值面。
    """
    lateSplatter.SetScaleFactor(0.005)

    lateSurface = vtkContourFilter()
    lateSurface.SetInputConnection(lateSplatter.GetOutputPort())
    lateSurface.SetValue(0, 0.01)

    lateMapper = vtkPolyDataMapper()
    lateMapper.SetInputConnection(lateSurface.GetOutputPort())
    lateMapper.ScalarVisibilityOff()

    lateActor = vtkActor()
    lateActor.SetMapper(lateMapper)
    lateActor.GetProperty().SetColor(colors.GetColor3d('Red'))

    bounds = popSplatter.GetOutput().GetBounds()

    axes = vtkAxes()
    # SetOrigin 设置原点坐标
    axes.SetOrigin(bounds[0], bounds[2], bounds[4])  # xmin,ymin, zmin
    # SetScaleFactor 设置坐标轴(vtkAxes)的长度
    axes.SetScaleFactor(popSplatter.GetOutput().GetLength() / 5)
    # axes.GetOutput()  -> PolyData

    axesTubes = vtkTubeFilter()
    axesTubes.SetInputConnection(axes.GetOutputPort())
    axesTubes.SetRadius(axes.GetScaleFactor() / 25.0)
    axesTubes.SetNumberOfSides(6)

    axesMapper = vtkPolyDataMapper()
    axesMapper.SetInputConnection(axesTubes.GetOutputPort())

    axesActor = vtkActor()
    axesActor.SetMapper(axesMapper)

    # Graphics stuff.
    renderer = vtkRenderer()

    renWin = vtkRenderWindow()
    renWin.AddRenderer(renderer)

    interactor = vtkRenderWindowInteractor()
    interactor.SetRenderWindow(renWin)

    # Set up the renderer.
    renderer.AddActor(lateActor)
    renderer.AddActor(axesActor)
    renderer.AddActor(popActor)
    renderer.SetBackground(colors.GetColor3d('Wheat'))
    renWin.SetSize(640, 480)
    renWin.SetWindowName('Finance')

    renderer.ResetCamera()
    renderer.GetActiveCamera().Dolly(1.3)
    renderer.ResetCameraClippingRange()

    # Interact with the data.
    renWin.Render()
    interactor.Start()

    print("")


if __name__ == '__main__':
    main()
相关推荐
共享家95271 天前
搭建 AI 聊天机器人:”我的人生我做主“
前端·javascript·css·python·pycharm·html·状态模式
Hgfdsaqwr1 天前
Python在2024年的主要趋势与发展方向
jvm·数据库·python
一晌小贪欢1 天前
Python 测试利器:使用 pytest 高效编写和管理单元测试
python·单元测试·pytest·python3·python测试
小文数模1 天前
2026年美赛数学建模C题完整参考论文(含模型和代码)
python·数学建模·matlab
Halo_tjn1 天前
基于封装的专项 知识点
java·前端·python·算法
Hgfdsaqwr1 天前
掌握Python魔法方法(Magic Methods)
jvm·数据库·python
weixin_395448911 天前
export_onnx.py_0130
pytorch·python·深度学习
s1hiyu1 天前
使用Scrapy框架构建分布式爬虫
jvm·数据库·python
2301_763472461 天前
使用Seaborn绘制统计图形:更美更简单
jvm·数据库·python
无垠的广袤1 天前
【VisionFive 2 Lite 单板计算机】边缘AI视觉应用部署:缺陷检测
linux·人工智能·python·opencv·开发板