12
$\begingroup$

We are planning to analyze geometry of artery branch pattern. We convert the binarized image into graph object using MorphologicalGraph[]. To avoid the complexity of the code for later analysis, we want to save the graph objects to a separate file. We tried

Export["graphList.m", obtainBranchGraphList]

and tried to use the object later. When we import the data using

g2 = Import["graphList.m"]

resulting g2 seem to contain all the information of the graph. However, the content of g2 is something like

Graph[{3, 5, 4, 13, 17, 26, 21, 42}, {3 \[UndirectedEdge] 5, 
  4 \[UndirectedEdge] 5, 17 \[UndirectedEdge] 26, 
  21 \[UndirectedEdge] 26, 21 \[UndirectedEdge] 5, 
  42 \[UndirectedEdge] 26, 
    21 \[UndirectedEdge] 
     13}, {VertexLabels -> {"Name", 8 -> None, 12 -> None, 22 -> None, 
      14 -> None, 2 -> None, 29 -> None, 41 -> None, 38 -> None, 
      37 -> None, 20 -> None, 32 -> None, 18 -> None, 25 -> None, 
      7 -> None, 9 -> None, 19 -> None, 30 -> None, 16 -> None, 
      23 -> None, 39 -> None, 34 -> None, 40 -> None, 11 -> None, 
      44 -> None, 6 -> None, 36 -> None, 33 -> None, 1 -> None, 
      15 -> None, 35 -> None, 28 -> None, 24 -> None, 10 -> None, 
      27 -> None, 43 -> None, 31 -> None}, 
    VertexCoordinates -> {{2096.5, 1813.5}, {2093.5, 1795.5}, {2144.5, 
       1810.5}, {2243.5, 1600.5}, {1892.5, 1581.5}, {1926.5, 
       1559.5}, {1942.5, 1571.5}, {1713.5, 1298.5}}}]

and we could not reconstruct the graph from g2. Is there any way to convert this text to graph object?

Thank you very much in advance.

Takashi.

$\endgroup$
  • $\begingroup$ if you comment out VertexLabels -> ..., part or change it to VertexLabels -> "Name" it works fine. $\endgroup$ – kglr Aug 14 '18 at 8:04
  • $\begingroup$ see also DumpSave and Get, e.g. DumpSave["graphList.mx", obtainBranchGraphList] and Get["graphList.mx"] $\endgroup$ – kglr Aug 14 '18 at 8:11
  • $\begingroup$ @kglr DumpSave will only hide the internal corruption temporarily. The bug is still there and will no doubt surface in a nastier way later. $\endgroup$ – Szabolcs Aug 14 '18 at 10:06
  • $\begingroup$ @Szabolcs, right; I didn't mean to suggest DumpSave/Get as a fix; just as an alternative to Export/Import. $\endgroup$ – kglr Aug 14 '18 at 19:49
11
$\begingroup$

This is, sadly, due to some long-standing bugs in Mathematica that I have reported multiple times, but Wolfram fails to take seriously. I don't understand how they can seriously keep marketing Mathematica for graph/network analysis for as long as even basic things don't work.

Here's what happens, in short:

There are several operations (such as VertexDelete, EdgeDelete, etc.) which will corrupt the internal structure of the graph whenever properties are present. Properties can be something as harmless looking as vertex labels.

Graph is an atomic object, and it is not represented internally through its InputForm. However, when you export it (as well as in several other situations) it does need to be converted to input form. When you re-import it, Mathematica notices that the structure is corrupted and refuses to construct the graph.

I do not know which specific bug (or which function) corrupted your graph. It is likely one of the ones I mentioned (edge/vertex deletions or additions).

However, I can see that vertex labels are assigned to non-existent vertices, such as vertex 8. You probably VertexDeleted these, which may have also triggered adding the v -> None rules to the vertex labels (yet another long-standing bug).

You can recover your graph by deleting the VertexLabels -> ... bit, e.g. using

DeleteCases[yourImportedGraph, VertexLabels -> _, Infinity]

But once again: this is not your fault, it's due to a set of bugs affecting even the most fundamental operations one would want to do with graphs in Mathematica. So please do complain and report it to Wolfram Support.

$\endgroup$
  • $\begingroup$ Thank you very much for your reply! It makes sense since we used VertexDelete[] to simplify the network structure. And we succeeded in recovering the original graph using DeleteCases[]. $\endgroup$ – Takashi Miura Aug 14 '18 at 10:14

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.