Example: Custom conversion from FreeGraph to DirectedGraph

Code example about customized FreeGraph conversion to DGML.

using imbSCI.Graph.FreeGraph;
using imbSCI.Graph.DGML;
using imbSCI.Graph.DGML.core;

imbSCI.Graph library is available via NuGet.

/// <summary>
/// Generates simple-styled graph
/// </summary>
/// <param name="addLegend">if set to <c>true</c> [add legend].</param>
/// <returns></returns>
public DirectedGraph GetSimpleGraph(Boolean addLegend=true)
  DirectedGraph output = new DirectedGraph();
  output.Title = name;
  output.Layout = imbSCI.Graph.DGML.enums.GraphLayoutEnum.Sugiyama;
  output.NeighborhoodDistance = 20;

  var res = output.Categories.AddOrGetCategory(0, "Reserve Term", "");
  var sec = output.Categories.AddOrGetCategory(1, "Secondary Term", "");
  var prim = output.Categories.AddOrGetCategory(2, "Primary Term", "");

  res.Stroke = Color.LightGray.toHexColor();
  res.StrokeThinkness = 2;
  res.Background = Color.WhiteSmoke.toHexColor();
  res.StrokeDashArray = "5,2,5,2";
  sec.Stroke = Color.Gray.toHexColor();
  sec.StrokeThinkness = 4;
  sec.Background = Color.WhiteSmoke.toHexColor();
  prim.Stroke = Color.Black.toHexColor();
  prim.StrokeThinkness = 6;
  prim.Background = Color.WhiteSmoke.toHexColor();

  foreach (freeGraphNodeBase node in nodes)
    Node nd = output.Nodes.AddNode(node.name, node.name + " " + node.weight.ToString("F4"));
    nd.Category = node.type.ToString();

  foreach (freeGraphLinkBase link in links)
    Link l = new Link(link.nodeNameA, link.nodeNameB);

    freeGraphNodeBase tn = GetNode(link.nodeNameB);

  if (addLegend)
    var legendNode = output.Nodes.AddNode("TermCategory", "LEGEND");
    var resNode = output.Nodes.AddNode("TCRES", "Reserve Term");
    resNode.Category = res.Id;
    var secNode = output.Nodes.AddNode("TCSEC", "Secondary Term");
    secNode.Category = sec.Id;
    var primNode = output.Nodes.AddNode("TCPRIM", "Primary Term");
    primNode.Category = prim.Id;

    output.Links.AddLink(legendNode, resNode, "Term Category");
    output.Links.AddLink(legendNode, secNode, "Term Category");
    output.Links.AddLink(legendNode, primNode, "Term Category");

  return output;

Now, application of the method:

var dgml = cloud.GetSimpleGraph(true);
dgml.Save(matrixReport.pathFor("cloud_initial_" + classSet.name, imbSCI.Data.enums.getWritableFileMode.none, "Initial version of full-sample set, diagnostic Semantic Cloud for category [" + classSet.name + "]"));

Where the matrixReport is folderNode instance (imbSCI.Core). The example is taken from actual code of imbWBIWeb Business Intelligence libraries of imbVeles Framework..Core reporting procedure.

folderNode classReportFolder = experimentContext.folder.Add("General", "General and diagnostic reports", "The folder contains general (outside k-folds) reports on analysied industries (categories), web sites and other diagnostic data");
folderNode matrixReport = classReportFolder.Add("clouds", "More reports on semantic cloud", "Directory contains exported DirectedGraphs, varous matrix derivates, combined cloud and other diagnostic things");



Render below, is made with Visual Studio 2017.

DirectedGraph, converted from imbNLP.PartOfSpeech’s lemmaSemanticCloud:FreeGraph

Spread the love