TTK
Loading...
Searching...
No Matches
ttkClusteringMetrics.cpp
Go to the documentation of this file.
2
3#include <vtkInformation.h>
4
5#include <vtkDataArray.h>
6#include <vtkDataSet.h>
7#include <vtkDoubleArray.h>
8#include <vtkIntArray.h>
9#include <vtkObjectFactory.h>
10#include <vtkPointData.h>
11#include <vtkSmartPointer.h>
12#include <vtkTable.h>
13#include <vtkVariantArray.h>
14
15#include <ttkMacros.h>
16#include <ttkUtils.h>
17
18// A VTK macro that enables the instantiation of this class via ::New()
19// You do not have to modify this
21
23 this->SetNumberOfInputPorts(2);
24 this->SetNumberOfOutputPorts(1);
25}
26
28 vtkInformation *info) {
29 if(port == 0) {
30 info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkTable");
31 return 1;
32 } else if(port == 1) {
33 info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkTable");
34 return 1;
35 }
36 return 0;
37}
38
40 vtkInformation *info) {
41 if(port == 0) {
42 info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkTable");
43 return 1;
44 }
45 return 0;
46}
47
48int ttkClusteringMetrics::RequestData(vtkInformation *ttkNotUsed(request),
49 vtkInformationVector **inputVector,
50 vtkInformationVector *outputVector) {
51
52 // Get input object from input vector
53 vtkTable *input1 = vtkTable::GetData(inputVector[0]);
54 vtkTable *input2 = vtkTable::GetData(inputVector[1]);
55 vtkTable *output = vtkTable::GetData(outputVector);
56 if(!input1 || !input2 || !output)
57 return 0;
58 // We do not use the GetInputArrayToProcess but a version of it allowing us to
59 // retrieve an array of int instead of an array of double.
60 vtkDataArray *inputClustering1 = this->GetInputArrayToProcess(0, input1);
61 vtkDataArray *inputClustering2 = this->GetInputArrayToProcess(1, input2);
62
63 if(!inputClustering1) {
64 this->printErr("Unable to retrieve input array for first clustering.");
65 return 1;
66 }
67 if(!inputClustering2) {
68 this->printErr("Unable to retrieve input array for second clustering.");
69 return 1;
70 }
71
72 vtkIntArray *intArray1 = vtkIntArray::SafeDownCast(inputClustering1);
73 vtkIntArray *intArray2 = vtkIntArray::SafeDownCast(inputClustering2);
74
75 // To avoid making a copy of the data.
76 const int *values1 = ttkUtils::GetPointer<int>(inputClustering1);
77 const int *values2 = ttkUtils::GetPointer<int>(inputClustering2);
78
79 // If all checks pass then log which array is going to be processed.
80 this->printMsg("Starting computation...");
81 this->printMsg(" First clustering column: "
82 + std::string(intArray1->GetName()));
83 this->printMsg(" Second clustering column: "
84 + std::string(intArray2->GetName()));
85
86 size_t const nbVal1 = inputClustering1->GetNumberOfTuples();
87 size_t const nbVal2 = inputClustering2->GetNumberOfTuples();
88
89 if(nbVal1 != nbVal2) {
90 this->printMsg("Error : the two clusterings must have the same size\n");
91 return 0;
92 }
93 size_t const nbVal = nbVal1;
94
95 double nmiValue = 0, ariValue = 0;
96 this->execute(values1, values2, nbVal, nmiValue, ariValue);
97 vtkNew<vtkDoubleArray> nmiValArray{}, ariValArray{};
98 output->SetNumberOfRows(1);
99
100 nmiValArray->SetName("NMIValue");
101 nmiValArray->SetNumberOfTuples(1);
102 nmiValArray->SetTuple1(0, nmiValue);
103 output->AddColumn(nmiValArray);
104
105 ariValArray->SetName("ARIValue");
106 ariValArray->SetNumberOfTuples(1);
107 ariValArray->SetTuple1(0, ariValue);
108 output->AddColumn(ariValArray);
109
110 // return success
111 return 1;
112}
#define ttkNotUsed(x)
Mark function/method parameters that are not used in the function body at all.
Definition BaseClass.h:47
TTK VTK-filter that wraps the ttk::ClusteringMetrics module.
int RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) override
int FillInputPortInformation(int port, vtkInformation *info) override
int FillOutputPortInformation(int port, vtkInformation *info) override
int execute(const int *clustering1, const int *clustering2, const size_t n, double &nmiValue, double &ariValue) const
int printErr(const std::string &msg, const debug::LineMode &lineMode=debug::LineMode::NEW, std::ostream &stream=std::cerr) const
Definition Debug.h:149
vtkStandardNewMacro(ttkClusteringMetrics)
printMsg(debug::output::BOLD+" | | | | | . \\ | | (__| | / __/| |_| / __/|__ _|"+debug::output::ENDCOLOR, debug::Priority::PERFORMANCE, debug::LineMode::NEW, stream)