Graph tool GraphML support for coloured vertices? - graph-tool

I am trying to use graph-tool to graph a network graph with coloured vertices. I am trying to graph the following graphML file from here shown below.
However, the colour are not showing with the following code:
g = Graph()
g= load_graph("filename.graphml", fmt="graphml")
graph_draw(g)
The graph renders but there are no colours on vertices, only for default red. I thought that graphML was fully supported?
Graph-tool docs states: "The only file formats which are capable of perfectly preserving the internal property maps are “gt” and “graphml”. Because of this, they should be preferred over the other formats whenever possible."
Is colour not an internal property?
Oringinally I was graphing in DOT. I have an array of colours the index of which depends on name of the node - the nodes are integers in increasing order. However, when I was using:
for v in g.vertices():
v_prop[v] = colourarray[vertex]
The colours did not correspond to the correct node. This is due to the fact the load_graph seems to have its own idea of which nodes are which index. Does any one have an idea of what I can do here?
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<key id="d0" for="node" attr.name="color" attr.type="string">
<default>yellow</default>
</key>
<key id="d1" for="edge" attr.name="weight" attr.type="double"/>
<graph id="G" edgedefault="undirected">
<node id="n0">
<data key="d0">green</data>
</node>
<node id="n1"/>
<node id="n2">
<data key="d0">blue</data>
</node>
<node id="n3">
<data key="d0">red</data>
</node>
<node id="n4"/>
<node id="n5">
<data key="d0">turquoise</data>
</node>
<edge id="e0" source="n0" target="n2">
<data key="d1">1.0</data>
</edge>
<edge id="e1" source="n0" target="n1">
<data key="d1">1.0</data>
</edge>
<edge id="e2" source="n1" target="n3">
<data key="d1">2.0</data>
</edge>
<edge id="e3" source="n3" target="n2"/>
<edge id="e4" source="n2" target="n4"/>
<edge id="e5" source="n3" target="n5"/>
<edge id="e6" source="n5" target="n4">
<data key="d1">1.1</data>
</edge>
</graph>
</graphml>

You are conflating the existence of a property map with it actually being used for drawing. If you want to use a property map, you have to do so explicitly:
graph_draw(g, vertex_fill_color=g.vp.color)
Please take a look at the documentation, which contains many examples of this kind.
The inherent vertex "names" that exist in dot and graphml files are loaded as internal property maps. In the case of dot, the property map is named "vertex_name":
g.vp.vertex_name
in the case of graphml, if the vertex label is not canonical (i.e. numbered from 0 to N-1), it is stored as "_graphml_vertex_id":
g.vp._graphml_vertex_id

Related

XML Literals in VB.NET Code don't seem work with #if statements

I have a vb.net project that uses an XML literal to configure the user interface navigation bar. The code looks something like this and has been working for many years.
Private ReadOnly _actionTreeXml As XElement =
<nodes>
<node key="any" name="Top">
<node key="log" name="***LOGIN***" type="everybody"></node>
<node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
<node key="barcode" name="Barcode Entry" ctrl="EditMfgEntry" type="mfg"></node>
<node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
<node key="readme" name="Version Info" type="everybody"></node>
</node>
</nodes>
I recently needed to have two builds of the project that differ slightly (don't want to pay for library code for many users). So I have one build where I #if out all the ui tools related to the barcode. This works great everwhere except in XML literals like this
#if USE_BAR_CODE=1
<node key="barcode" name="Barcode Entry" ctrl="EditMfgEntry" type="mfg"> </node>
#end if
If I set USE_BAR_CODE to 0 I still get the xml literal inside the #if block, but everywhere in my code where I #if'ed regular VB source the code was not compiled.
This leads me to believe that the compliation process handles xml literals BEFORE #if statements. Am I missing something?
Unfortunately no, you can't use directives like this in literals. Under the hood, the behavior is the same as the If, Then and Else statements. You can't put those directly in line the way you are, you have to wrap the code block to tell the compiler what to compile versus run time compile like the standard If, Then and Else statements. The statements within a conditional compilation block must be complete logical statements. For example, you cannot conditionally compile only the attributes of literals, functions etc...
So a quick solution is below to either include the node or not include it. This was tested as well and works just fine.
#If USE_BAR_CODE = 1 Then
Private ReadOnly _actionTreeXml As XElement = <nodes>
<node key="any" name="Top">
<node key="log" name="***LOGIN***" type="everybody"></node>
<node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
<node key="barcode" name="Barcode Entry" ctrl="EditMfgEntry" type="mfg"></node>
<node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
<node key="readme" name="Version Info" type="everybody"></node>
</node>
</nodes>
#Else
Private ReadOnly _actionTreeXml As XElement = <nodes>
<node key="any" name="Top">
<node key="log" name="***LOGIN***" type="everybody"></node>
<node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
<node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
<node key="readme" name="Version Info" type="everybody"></node>
</node>
</nodes>
#End If
May be not the solution at all, but as proverb says "A bad bush is better than the open field". 😉
The main idea is to use processing instruction node. If element has processing instruction above it, then skip it, otherwise include it. This way you can control the "inclusiveness" of that element.
1) First, create extension method:
Module XmlExtentions
<System.Runtime.CompilerServices.Extension>
Function Filter(root_node As IEnumerable(Of XElement)) As IEnumerable(Of XElement)
Return root_node.First().Elements().Where(
Function(e)
If e.PreviousNode Is Nothing Then Return True
Return e.PreviousNode.NodeType <> Xml.XmlNodeType.ProcessingInstruction
End Function)
End Function
End Module
2. Query our XML:
Dim _actionTreeXml As XElement =
<nodes>
<node key="any" name="Top">
<node key="log" name="***LOGIN***" type="everybody"></node>
<node key="op" name="Home" ctrl="uiHomePage" type="mfg"></node>
<?Skip-Element?>
<node key="barcode" name="Barcode Entry" ctrl="EditMfgEntry" type="mfg"></node>
<node key="wip" name="Work in Progress" ctrl="QueryWIP" type="mfg"></node>
<node key="readme" name="Version Info" type="everybody"></node>
</node>
</nodes>
Dim elements = _actionTreeXml.<node>.Filter()
'// Do something...
elements.ToList().ForEach(Sub(e) Console.WriteLine(e))

RelaxNG choice and regular expression

I have a RelaxNG based XML instance which should allow (choice) align="right", align="left" and align="{ anything here }". The right/left is easy:
attribute name="align">
<a:documentation>doc for attrib</a:documentation>
<choice>
<value>left</value>
<a:documentation>doc for left</a:documentation>
<value>right</value>
<a:documentation>doc for right</a:documentation>
</choice>
</attribute>
But how can I allow the user to enter { ... } as an alternative to the two values above?
You need to use the XML Schema string type and set a pattern on it. Here's an example:
<grammar xmlns="http://relaxng.org/ns/structure/1.0"
datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
<start>
<element name="top">
<oneOrMore>
<element name="foo">
<attribute name="align">
<choice>
<value>left</value>
<value>right</value>
<data type="string">
<param name="pattern">\{.+\}</param>
</data>
</choice>
</attribute>
</element>
</oneOrMore>
</element>
</start>
</grammar>
And a test file:
<top>
<!-- valid cases -->
<foo align="{fasf}"></foo>
<foo align="left"></foo>
<foo align="right"></foo>
<!-- invalid cases -->
<foo align="fasf"></foo>
<foo align="{}"></foo>
<foo align="a{c}b"></foo>
</top>
Note the comments that delimit valid and invalid cases.
The regular expression I used allows things like align="{ab{c}d}". Curly braces are valid inside the curly braces that start and end the attribute value. I've not see any reason to disallow it.

Trouble passing XML text to RIA Services from my client

I am trying to pass the following XML text to RIA services in a query operation that returns a queryable sequence of entities in response:
<?xml version="1.0" encoding="utf-8"?>
<project>
<item type="Item" filetype="cabinet" category="EZ Workshop" name="EZW Panel Edge-Banded" height="24.000000" width="36.000000" depth="0.725000" quantity="1">
</item>
<item type="Item" filetype="cabinet" category="EZ Furniture" name="Entry Bench" height="19.000000" width="48.000000" depth="17.999999" quantity="1">
</item>
<item type="Item" filetype="cabinet" category="EZ Closet Euro Style" name="CSEKD Tall H3R 28-72W 12-24D" height="84.000000" width="54.000000" depth="19.999999" quantity="1">
</item>
<item type="Item" filetype="assembly" category="EZ Pro ManCave" name="EZ Corn Hole Game-Set" height="0" width="0" depth="0" quantity="1">
</item>
<item type="Item" filetype="assembly" category="EZ Office" name="EZ 30 Printer Stand" height="0" width="0" depth="0" quantity="1">
</item>
<item type="Item" filetype="assembly" category="Corporate Culture Pro" name="C-Table" height="0" width="0" depth="0" quantity="1">
</item>
</project>
This is the query operation:
[Query]
public IQueryable<ProjectItem> GetItemsFromImport(String a_strImportXml)
{
// Return empty sequence for now as a test.
return new ProjectItem[0].AsQueryable();
}
When I pass the full XML, I get that annoying "Not Found" exception, and the break-point in my operation is never hit. I'm using Visual Studio 2010's ASP.NET Development Server. When I get "Not Found" with that it portends bad stuff. The kicker is, when I pass an empty string, I get no exceptions at all, and the break-point is hit.
As you can see its not a tremendously long XML document. Is there some limit to the amount of data sent? Do I have to escape the document?
Thanks.
Edits:
I discovered that I only had to escape the structural characters ('<', '>', and '&') out of the document before I sent it. I'm using String.Replace to do it. Does anyone know if there is a better way to accomplish this. Something similar to Uri.EscapeDataString perhaps?
var strImportXml = a_xImport.ToString();
strImportXml = strImportXml.Replace("&", "&");
strImportXml = strImportXml.Replace("<", "<");
strImportXml = strImportXml.Replace(">", ">");
Ok, so I guess this is standing as the answer to my own question. I discovered that I only had to escape the structural characters ('<', '>', and '&') out of the document before I sent it. I'm using String.Replace to do it. Does anyone know if there is a better way to accomplish this. Something similar to Uri.EscapeDataString perhaps?
var strImportXml = a_xImport.ToString();
strImportXml = strImportXml.Replace("&", "&");
strImportXml = strImportXml.Replace("<", "<");
strImportXml = strImportXml.Replace(">", ">");
NOTE: I'd still like to know if there is a better way to do this.

LINQ to XML. Enumeration yielded no results

I'm having trouble populating an object from an XML file. I've copied an example I've found almost exactly, with variable names changed, but I keep getting the "Enumeration yielded no results" exception.
Here is my code:
Dim element As XElement = XElement.Load(path)
Dim itemProps = From p In element...<Property> _
Where p.<LanguageCode>.Value = "en_us" _
Select p.<Title>.Value, p.<Description>.Value
Using breakpoints, I have confirmed that the 'element' variable is being properly populated using the XElement.Load(path) method.
Here is the XML file that is being accessed:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Items>
<Item ItemID="1">
<Property ItemPropertyID="1">
<Title>Title1</Title>
<Description>Description1</Description>
<LanguageCode>en-us</LanguageCode>
</Property>
</Item>
<Item ItemID="2">
<Property ItemPropertyID="2">
<Title>Title2</Title>
<Description>Description2</Description>
<LanguageCode>en-us</LanguageCode>
</Property>
</Item>
<Item ItemID="3">
<Property ItemPropertyID="3">
<Title>Title3</Title>
<Description>Description3</Description>
<LanguageCode>en-us</LanguageCode>
</Property>
</Item>
<Item ItemID="4">
<Property ItemPropertyID="4">
<Title>Title4</Title>
<Description>Description4</Description>
<LanguageCode>en-us</LanguageCode>
</Property>
</Item>
<Item ItemID="5">
<Property ItemPropertyID="5">
<Title>Title5</Title>
<Description>Description5</Description>
<LanguageCode>en-us</LanguageCode>
</Property>
</Item>
<Item ItemID="6">
<Property ItemPropertyID="6">
<Title>Title6</Title>
<Description>Description6</Description>
<LanguageCode>en-us</LanguageCode>
</Property>
</Item>
</Items>
Essentially, the XML query is supposed to return the title and the description for every Property which has an element called Language Code, which is equal to "en-us". I have a feeling that my problem lies in my XML code.
This language code:
en_us
should be:
en-us
Try taking one of the dots out of
Dim itemProps = From p In element...<Property>
Your going 3 levels down, when you only need to go down 2.
If that doesn't work try just one dot, because essentially the path your travelling is only 1 below the root of the document.

Is there a snippet for an auto-property in VB.NET?

In Visual Studio 2010, for VB.NET, the "Property" + Tab + Tab inserts a full property implementation. Is there another snippet for inserting an autoproperty?
Just put this in a file called C:\Users\\Documents\Visual Studio 2010\Code Snippets\Visual Basic\My Code Snippets\DefaultProperty.snippet and restart VS... or put it in that file but not in that dir, then in VS click Tools, Code Snippets Manager, and Select Visual Basic as the language... the click on the Import button. Select your new file, and then choose the top folder "My Snippets". Now in the IDE just type PropDefAuto and tab tab. Feel free to modify the file.
<?xml version="1.0" encoding="UTF-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Define a Auto-Implemented Default Property</Title>
<Author>Some Guy On SO</Author>
<Description>Defines an Auto-Implemented default property or index property.</Description>
<Keywords>
<Keyword>PropAuto</Keyword>
</Keywords>
<Shortcut>PropDefAuto</Shortcut>
</Header>
<Snippet>
<Imports>
<Import>
<Namespace>System</Namespace>
</Import>
</Imports>
<Declarations>
<Literal>
<ID>propertyName</ID>
<Type>
</Type>
<ToolTip>Rename to descriptive name for the property.</ToolTip>
<Default>PropertyName</Default>
</Literal>
<Literal>
<ID>indexType</ID>
<Type>
</Type>
<ToolTip>Replace with the type of the index</ToolTip>
<Default>Integer</Default>
</Literal>
<Literal>
<ID>propertyType</ID>
<Type>
</Type>
<ToolTip>Replace with the type returned by the property.</ToolTip>
<Default>String</Default>
</Literal>
<Literal>
<ID>IndexIsValid</ID>
<Type>Boolean</Type>
<ToolTip>Replace with an expression to test if index is valid.</ToolTip>
<Default>True</Default>
</Literal>
</Declarations>
<Code Language="VB" Kind="method decl"><![CDATA[Public Property $propertyName$ As $PropertyType$
]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
The closest you can currently get in VB is typing out
Property Name as string
You can find more information in this blog post by The Gu