This example first loads an ensemble of twelve scalar fields inside a cinema database from disk. Two scalar fields are extracted from the ensemble.
Then, the PersistenceDiagram is computed on both fields.
PersistenceDiagramClustering is then used to compute the Wasserstein distance between the two persistence diagrams.
The assignment problem associated to the distance is computed using the Auction algorithm, and the corresponding matchings are displayed in ParaView.
The actual distance is available in the field data of the 'matchings' output.
The python script computes the distance and prints it in the terminal output. Additionally, it saves the distance as a .csv file.
#! /usr/bin/env/pythonfromparaview.simpleimport*importnumpyasnp# create a new 'TTK CinemaReader'tTKCinemaReader1=TTKCinemaReader(DatabasePath="Isabel.cdb")# create a new 'TTK CinemaProductReader'tTKCinemaProductReader1=TTKCinemaProductReader(Input=tTKCinemaReader1)# create a new 'Extract Block'extractBlock1=ExtractBlock(Input=tTKCinemaProductReader1)extractBlock1.Selectors=["/Root/Block0","/Root/Block10"]# create a new 'Calculator'calculator1=Calculator(Input=extractBlock1)calculator1.ResultArrayName="velocityMag"calculator1.Function="-velocityMag"# create a new 'TTK PersistenceDiagram'tTKPersistenceDiagram3=TTKPersistenceDiagram(Input=calculator1)tTKPersistenceDiagram3.ScalarField=["POINTS","velocityMag"]tTKPersistenceDiagram3.InputOffsetField=["POINTS","velocityMag"]tTKPersistenceDiagram3.IgnoreBoundary=False# create a new 'Threshold'threshold7=Threshold(Input=tTKPersistenceDiagram3)threshold7.Scalars=["CELLS","Persistence"]threshold7.LowerThreshold=1.2threshold7.UpperThreshold=136.490955383565# create a new 'TTK PersistenceDiagramClustering'tTKPersistenceDiagramClustering1=TTKPersistenceDiagramClustering(Input=threshold7)UpdatePipeline()# Get the datamatchings_data=FetchData(OutputPort(tTKPersistenceDiagramClustering1,2))[0]# Get field datafield_data=matchings_data.GetBlock(0).GetFieldData()# Display the Wasserstein distancewasserstein_distance=field_data.GetArray("WassersteinDistance").GetValue(0)# Save the Wasserstein distance in a csv filenp.savetxt("WassersteinDistance.csv",[wasserstein_distance])
To run the above Python script, go to your ttk-data directory and enter the following command: