Public Class KnowledgeGraphCentralityScores
public class KnowledgeGraphCentralityScores
Public Class KnowledgeGraphCentralityScores
public class KnowledgeGraphCentralityScores
{
//using ArcGIS.Core.Data.Knowledge.Extensions;
await QueuedTask.Run(() =>
{
//using(var kg = ....) {
//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()}");
}
}
});
}
{
//using ArcGIS.Core.Data.Knowledge.Extensions;
await QueuedTask.Run(() =>
{
///var kgConfig = ...;
//var kgSubgraph = ...;
//var measures = ...;
//using(var kg = ...) {
var results = kg.ComputeCentrality(kgConfig, kgSubgraph, 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
}
}
}
});
}
{
//using ArcGIS.Core.Data.Knowledge.Extensions;
await QueuedTask.Run(() =>
{
//using(var kg = ...) {
//var kgResults = kg.ComputeCentrality(...);
var count_entities = kgResults.RawUids.Count();
var score_len = kgResults.Scores.RawScores.Length;
//Find the max and min score for each measure w/out
//using LINQ or sorting
for (int m = 0; m < kgResults.Scores.Measures.Length; m++)
{
//index into the scores array for the current measure
var start_idx = count_entities * m;
var end_idx = start_idx + count_entities;
double max_score = double.MinValue;
double min_score = double.MaxValue;
List<object> max_uids = new List<object>();
List<object> min_uids = new List<object>();
for (int i = 0, s = start_idx; s < end_idx; i++, s++)
{
var score = kgResults.Scores.RawScores[s];
//max
if (score > max_score)
{
max_score = score;
max_uids.Clear();
max_uids.Add(kgResults.RawUids[i]);
}
else if (score == max_score)
{
//Collect all uids with the max score
max_uids.Add(kgResults.RawUids[i]);
}
//min
if (score < min_score)
{
min_score = score;
min_uids.Clear();
min_uids.Add(kgResults.RawUids[i]);
}
else if (score == min_score)
{
//Collect all uids with the min score
min_uids.Add(kgResults.RawUids[i]);
}
}
//TODO - use the max and min scores and uids for
//the current measure "measures[m]"
//max_score, max_uids
//min_score, min_uids
}
});
}
{
//using ArcGIS.Core.Data.Knowledge.Extensions;
await QueuedTask.Run(() =>
{
//using(var kg = ...) {
//var kgResults = kg.ComputeCentrality(...);
var count_entities = kgResults.RawUids.Count();
var score_len = kgResults.Scores.RawScores.Length;
//Find the max and min score for each measure using LINQ
for (int m = 0; m < kgResults.Scores.Measures.Length; m++)
{
//index into the scores array for the current measure
var start_idx = count_entities * m;
var end_idx = start_idx + count_entities;
//Get the scores for the specified measure
var scores = Enumerable.Range(start_idx, count_entities)
.Select(i => kgResults.Scores.RawScores[i]).ToArray();
//max + min
var max_score = scores.Max();
var min_score = scores.Min();
//uids with the max score
var max_uids = Enumerable.Range(0, count_entities)
.Where(i => scores[i] == max_score)
.Select(i => kgResults.RawUids[i])
.ToList();
//uids with the min score
var min_uids = Enumerable.Range(0, count_entities)
.Where(i => scores[i] == min_score)
.Select(i => kgResults.RawUids[i])
.ToList();
//TODO - use the max and min scores and uids for
//the current measure "measures[m]"
//max_score, max_uids
//min_score, min_uids
}
});
}
{
//using ArcGIS.Core.Data.Knowledge.Extensions;
//Note: logic uses the following custom class:
//
//public class KG_Score_Value {
// public KG_Score_Value(double score, int uid_idx) {
// Score = score;
// Uid_idx = uid_idx;
// }
//
// public double Score { get; set; }
// public int Uid_idx { get; set; }
//}
//var kgResults = kg.ComputeCentrality(...);
var count_entities = kgResults.RawUids.Count();
var score_len = kgResults.Scores.RawScores.Length;
int n = 100;
//Find the top and bottom "n" scores for each measure
for (int m = 0; m < kgResults.Scores.Measures.Length; m++)
{
//index into the scores array for the current measure
var start_idx = count_entities * m;
var end_idx = start_idx + count_entities;
//Get the scores for the specified measure
var scores = Enumerable.Range(start_idx, count_entities)
.Select(i => kgResults.Scores.RawScores[i]).ToArray();
//use "KG_Score_Value" to hold the score and uid index
var uid_idx = 0;
var kg_score_vals = Enumerable.Range(start_idx, count_entities)
.Select(i => new KG_Score_Value(kgResults.Scores.RawScores[i], uid_idx++))
.OrderByDescending(k => k.Score)
.ToArray();
//get up to the top or bottom N scores
if (n > kg_score_vals.Length)
n = kg_score_vals.Length;
//get the top N scores and uids - largest first
var top_n = kg_score_vals.Take(n).ToList();
//get the bottom N scores and uids - smallest first
var bottom_n = kg_score_vals.TakeLast(n)
.Reverse().ToList();
foreach (var kg_score in top_n)
{
//TODO - use top "n" scores
var score = kg_score.Score;
var uid1 = kgResults.RawUids[kg_score.Uid_idx];
//...use it
}
foreach (var kg_score in bottom_n)
{
//TODO - use bottom "n" scores
var score = kg_score.Score;
var uid1 = kgResults.RawUids[kg_score.Uid_idx];
//...use it
}
}
}
System.Object
ArcGIS.Core.Data.Knowledge.Analytics.KnowledgeGraphCentralityScores
Target Platforms: Windows 11 Home, Pro, Enterprise (64 bit)