In [32]:
import networkx as nx
from decimal import *
from re import compile

In [2]:
g = nx.Graph()

In [3]:
g.add_node(1)

In [4]:
g.add_node(2)

In [5]:
g.add_edge(1,2)

In [6]:
%matplotlib inline

In [7]:
nx.draw(g)



In [8]:
g.node[1]['weight'] = str(1e6)

In [16]:
g.edge[1][2]['weight'] = Decimal(0.00000000000000001)

In [17]:
for line in nx.generate_gml(g):
    print line


graph [
  node [
    id 0
    label "1"
    weight "1000000.0"
  ]
  node [
    id 1
    label "2"
  ]
  edge [
    source 0
    target 1
    weight 1.00000000000000007154242405462192450852805618492324772617063644020163337700068950653076171875E-17
  ]
]

In [18]:
def remove_exponent(d):
    '''Remove exponent and trailing zeros.

    >>> remove_exponent(Decimal('5E+3'))
    Decimal('5000')

    '''
    return d.quantize(Decimal(1)) if d == d.to_integral() else d.normalize()

In [22]:
g.edge[1][2]['weight'] = remove_exponent(Decimal(1e12))

In [23]:
for line in nx.generate_gml(g):
    print line


graph [
  node [
    id 0
    label "1"
    weight "1000000.0"
  ]
  node [
    id 1
    label "2"
  ]
  edge [
    source 0
    target 1
    weight 1000000000000
  ]
]

In [49]:
def read_gml_and_normalize_floats(file):
    """
    Read a GML file line by line, looking for scientific notation
    and when found, normalize it using the Decimal library.
    Then pass the lines of text to networkx parse_gml to
    parse it.  This is a drop-in replacement for read_gml()
    """
    exp_regex = compile(r"(\d+(\.\d+)?)[Ee](\+|-)(\d+)")

    input_lines = []

    with open(file, 'rb') as gmlfile:
        for line in gmlfile:
            result = exp_regex.search(line)
            if result is not None:
                matched_value = result.group(0)
                replacement = str(remove_exponent(Decimal(float(matched_value))))
                line = line.replace(matched_value, replacement)
                print "Replacing %s with %s" %( matched_value, replacement)
                
            print line    
            input_lines.append(line)
    

    
    
    return nx.parse_gml(input_lines)

In [35]:
file = '/Users/mark/seriationct/experiments/seriationct-7/seriation-results/a7eb7f58-d34e-11e4-93bf-b8f6b1154c9b-0-sampled-0.1/a7eb7f58-d34e-11e4-93bf-b8f6b1154c9b-0-sampled-0.1-minmax-by-weight.png.gml'

In [28]:
g2 = nx.read_gml(file)


---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-28-ff365eda00d9> in <module>()
----> 1 g2 = nx.read_gml(file)

/Users/mark/anaconda/lib/python2.7/site-packages/networkx/readwrite/gml.pyc in read_gml(path, relabel)

/Users/mark/anaconda/lib/python2.7/site-packages/networkx/utils/decorators.pyc in _open_file(func, *args, **kwargs)
    196         if is_string_like(path):
    197             ext = splitext(path)[1]
--> 198             fobj = _dispatch_dict[ext](path, mode=mode)
    199             close_fobj = True
    200         elif hasattr(path, 'read'):

IOError: [Errno 2] No such file or directory: '/Users/mark/seriationct/experiments/seriationct-7/seriation-results/a7eb7f58-d34e-11e4-93bf-b8f6b1154c9b-0-sampled-0.1//Users/mark/seriationct/experiments/seriationct-7/seriation-results/a7eb7f58-d34e-11e4-93bf-b8f6b1154c9b-0-sampled-0.1'

In [50]:
g2 = read_gml_and_normalize_floats(file)


graph [

  is_directed 0

  node [

    id 0

    label "assemblage-31-49"

    xCoordinate 49.0

    size 312.263661735

    yCoordinate 31.0

    name "assemblage-31-49"

  ]

  node [

    id 1

    label "assemblage-6-34"

    xCoordinate 34.0

    size 342.678102186

    yCoordinate 6.0

    name "assemblage-6-34"

  ]

  node [

    id 2

    label "assemblage-22-14"

    xCoordinate 14.0

    size 312.263661735

    yCoordinate 22.0

    name "assemblage-22-14"

  ]

  node [

    id 3

    label "assemblage-34-68"

    xCoordinate 68.0

    size 312.263661735

    yCoordinate 34.0

    name "assemblage-34-68"

  ]

  node [

    id 4

    label "assemblage-30-41"

    xCoordinate 41.0

    size 619.755402642

    yCoordinate 30.0

    name "assemblage-30-41"

  ]

  node [

    id 5

    label "assemblage-8-19"

    xCoordinate 19.0

    size 668.278670854

    yCoordinate 8.0

    name "assemblage-8-19"

  ]

  node [

    id 6

    label "assemblage-53-70"

    xCoordinate 70.0

    size 1482.31068176

    yCoordinate 53.0

    name "assemblage-53-70"

  ]

  node [

    id 7

    label "assemblage-66-55"

    xCoordinate 55.0

    size 255.909549086

    yCoordinate 66.0

    name "assemblage-66-55"

  ]

  node [

    id 8

    label "assemblage-70-63"

    xCoordinate 63.0

    size 280.791707695

    yCoordinate 70.0

    name "assemblage-70-63"

  ]

  node [

    id 9

    label "assemblage-35-12"

    xCoordinate 12.0

    size 283.247586364

    yCoordinate 35.0

    name "assemblage-35-12"

  ]

  node [

    id 10

    label "assemblage-62-17"

    xCoordinate 17.0

    size 312.324840208

    yCoordinate 62.0

    name "assemblage-62-17"

  ]

  node [

    id 11

    label "assemblage-50-22"

    xCoordinate 22.0

    size 385.03108449

    yCoordinate 50.0

    name "assemblage-50-22"

  ]

  node [

    id 12

    label "assemblage-65-58"

    xCoordinate 58.0

    size 932.080242849

    yCoordinate 65.0

    name "assemblage-65-58"

  ]

  node [

    id 13

    label "assemblage-36-27"

    xCoordinate 27.0

    size 593.116547903

    yCoordinate 36.0

    name "assemblage-36-27"

  ]

  node [

    id 14

    label "assemblage-52-33"

    xCoordinate 33.0

    size 407.684598757

    yCoordinate 52.0

    name "assemblage-52-33"

  ]

  edge [

    source 0

    target 3

    inverseweight 1.22371548781

    weight 0.817183413926

  ]

  edge [

    source 1

    target 11

    inverseweight 103.291450201

    weight 0.00968134340308

  ]

  edge [

    source 1

    target 8

    inverseweight 519.331792222

    weight 0.00192555128528

  ]

  edge [

    source 2

    target 3

    inverseweight 1.20641874572

    weight 0.828899586939

  ]

  edge [

    source 3

    target 12

    inverseweight 5.87654893431

    weight 0.170167901464

  ]

  edge [

    source 4

    target 5

    inverseweight 23.6902150659

    weight 0.0422115205463

  ]

  edge [

    source 4

    target 12

    inverseweight 87.7844119726

    weight 0.0113915440968

  ]

  edge [

    source 5

    target 13

    inverseweight 22.2010650766

    weight 0.0450428840486

  ]

  edge [

    source 6

    target 7

    inverseweight 37924.8309323

Replacing 2.63679487928e-05 with 0.00002636794879279999928166529355
    weight 0.00002636794879279999928166529355

  ]

  edge [

    source 6

    target 10

    inverseweight 37924.8309323

Replacing 2.63679487928e-05 with 0.00002636794879279999928166529355
    weight 0.00002636794879279999928166529355

  ]

  edge [

    source 6

    target 8

    inverseweight 29301.4406957

Replacing 3.41280147412e-05 with 0.00003412801474120000314862663493
    weight 0.00003412801474120000314862663493

  ]

  edge [

    source 7

    target 10

Replacing 1e+12 with 1000000000000
    inverseweight 1000000000000

Replacing 1e-12 with 9.999999999999999798866476293E-13
    weight 9.999999999999999798866476293E-13

  ]

  edge [

    source 9

    target 12

    inverseweight 4.32126887322

    weight 0.231413510554

  ]

  edge [

    source 9

    target 14

    inverseweight 17.2464550455

    weight 0.0579829302523

  ]

  edge [

    source 11

    target 13

    inverseweight 69.7931634812

    weight 0.0143280509168

  ]

]


In [47]:
g2


Out[47]:
<networkx.classes.graph.Graph at 0x109db41d0>

In [51]:
nx.draw(g2)



In [ ]: