ArcGIS Pro 3.6 API Reference Guide
ArcGIS.Core.Data.Knowledge.Extensions Namespace / KnowledgeGraphExtensions Class / ComputeCentrality Method
The knowledge graph on which centrality analysis will be run
The configuration to be used
The subgraph specification
The measures to be computed
Example

In This Topic
    ComputeCentrality Method
    In This Topic
    Computes centrality measures for the specified knowledge graph and subgraph. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax

    Parameters

    kg
    The knowledge graph on which centrality analysis will be run
    configuration
    The configuration to be used
    subGraph
    The subgraph specification
    centralityMeasures
    The measures to be computed

    Return Value

    Exceptions
    ExceptionDescription
    This method or property must be called within the lambda passed to QueuedTask.Run.
    Remarks
    Note: ArcGIS.Core.CIM.CentralityMeasure.Coreness is incompatible with directed relationships, i.e. ArcGIS.Core.CIM.CentralityRelationshipInterpretation.Directed. Attempting to calculate a centrality analysis that includes this combination will throw a System.NotImplementedException
    Example
    Compute Centrality Using Defaults
    //using ArcGIS.Core.Data.Knowledge.Extensions;
    
    await QueuedTask.Run(() =>
    {
    
      //take default settings...
      //undirected relationship interpretation
      //use default relationship importance = 0
      //use default relationship cost = 0
      //use default Multiedge factor = 0
      //no normalization
      var kg_config = new CIMKnowledgeGraphCentralityConfiguration();
    
      //include all entities from the kg in the subgraph
      //(no filters)
      var kg_subgraph = new CIMKnowledgeGraphSubGraph();
    
      //include all centrality measures
      CentralityMeasure[] measures = [
        CentralityMeasure.Degree,
        CentralityMeasure.InDegree,
        CentralityMeasure.OutDegree,
        CentralityMeasure.Coreness,//Coreness only wks w/ undirected relates
        CentralityMeasure.Betweenness,
        CentralityMeasure.Closeness,
        CentralityMeasure.Harmonic,
        CentralityMeasure.Eigenvector,
        CentralityMeasure.PageRank
      ];
    
      //compute centrality
      var kg_centrality_results = kg.ComputeCentrality(
                                        kg_config, kg_subgraph, measures);
      //TODO - process results
    });
    
    Process Centrality Results
    //using ArcGIS.Core.Data.Knowledge.Extensions;
    
    await QueuedTask.Run(() =>
    {
    
      //use defaults...
      var kg_config = new CIMKnowledgeGraphCentralityConfiguration();
    
      //include all entities from the kg in the subgraph
      //(no filters)
      var kg_subgraph = new CIMKnowledgeGraphSubGraph();
    
      //include all centrality measures
      CentralityMeasure[] measures = [
        CentralityMeasure.Degree,
        CentralityMeasure.InDegree,
        CentralityMeasure.OutDegree,
        CentralityMeasure.Coreness,
        CentralityMeasure.Betweenness,
        CentralityMeasure.Closeness,
        CentralityMeasure.Harmonic,
        CentralityMeasure.Eigenvector,
        CentralityMeasure.PageRank
      ];
    
      //compute centrality
      var kg_centrality_results = kg.ComputeCentrality(
                                        kg_config, kg_subgraph, measures);
      //output results - results include measure scores for all entities
      //in all types in the subgraph
      System.Diagnostics.Debug.WriteLine("Centrality Results:");
      foreach (var named_type in kg_centrality_results.NamedTypes)
      {
        System.Diagnostics.Debug.WriteLine($"Named type: {named_type}");
        foreach (var uid in kg_centrality_results.GetUidsForNamedType(named_type))
        {
          //measure scores include one score per measure in the input measures array
          var scores = kg_centrality_results.Scores[uid];
          StringBuilder sb = new StringBuilder();
          var sep = "";
          //or use kg_centrality_results.Scores.Measures.Length
          //kg_centrality_results.Scores.Measures is there for convenience
          for (int m = 0; m < measures.Length; m++)
          {
            sb.Append($"{sep}{measures[m].ToString()}: {scores[m]}");
            sep = ", ";
          }
          System.Diagnostics.Debug.WriteLine($"  '{uid}' {sb.ToString()}");
        }
      }
    });
    Output Centrality Results
    //using ArcGIS.Core.Data.Knowledge.Extensions;
    
    await QueuedTask.Run(() =>
    {
      ///var config = ...;
      //var subGraph = ...;
      //var measures = ...;
    
      var results = kg.ComputeCentrality(config, subGraph, measures);
      //loop through each (entity) named type (relates are never included in results)
      foreach (var named_type in results.NamedTypes)
      {
        //Get the entity uids for each named type
        foreach (var uid in results.GetUidsForNamedType(named_type))
        {
          //Get the scores for each uid via the [] indexer on "Scores"
          var scores = results.Scores[uid];
          //There is one score per measure in the input measures array
          //Note: results.Scores.Measures is also provided for convenience...
          //for (int m = 0; m < results.Scores.Measures.Length; m++)
          for (int m = 0; m < measures.Length; m++)
          {
            var score = scores[m];//score for the given measure
                                    //TODO - use measure score
    
          }
        }
      }
    });
    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3.6 or higher.
    See Also