Commit 1e2e5ff6 authored by jay's avatar jay
Browse files

Candidate graph can save / load user defined adjacency

parent dbbdcd0f
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
{"AS15-M-0297_SML.png": ["AS15-M-0298_SML.png", "AS15-M-0300_SML.png", "AS15-M-0299_SML.png", "AS15-M-0296_SML.png"], "AS15-M-0300_SML.png": ["AS15-M-0298_SML.png", "AS15-M-0297_SML.png", "AS15-M-0299_SML.png"], "AS15-M-0299_SML.png": ["AS15-M-0298_SML.png", "AS15-M-0297_SML.png", "AS15-M-0300_SML.png", "AS15-M-0296_SML.png"], "AS15-M-0295_SML.png": [], "AS15-M-0296_SML.png": ["AS15-M-0298_SML.png", "AS15-M-0297_SML.png", "AS15-M-0299_SML.png"], "AS15-M-0298_SML.png": ["AS15-M-0297_SML.png", "AS15-M-0300_SML.png", "AS15-M-0299_SML.png", "AS15-M-0296_SML.png"]}
 No newline at end of file
+17 −0
Original line number Diff line number Diff line
@@ -27,3 +27,20 @@ def read_json(inputfile):
	except:
	    raise IOError
    return jdict

def write_json(outdata, outputfile):
    """
    Write a Python dictionary as a plain-text JSON file

    Parameters
    ==========
    outdata : dict
              The data structure to be serialized
    outputfile : str
                 The file to write the data to.
    """
    try:
        with open(outputfile, 'w') as f:
            f.write(json.dumps(outdata, outputfile))
    except:
        raise IOError('Unable to write data to {}'.format(outputfile))
+384 B (1.35 KiB)

File changed.

No diff preview for this file type.

+24 −0
Original line number Diff line number Diff line
try:
    import yaml
except:
    print 'YAML package not installed, disabling yaml_io module'

def read_yaml(inputfile):
    """
    Read the input yaml file into a python dictionary

    Parameters
    =========
    inputfile : str
                PATH to the file on disk

    Returns
    =======
    ydict : dict
            YAML file parsed to a Python dict
    """
    try:
        ydict = yaml.load(f.read())
    except:
        raise IOError('Unable to load YAML file.')
    return ydict
+36 −2
Original line number Diff line number Diff line
from collections import Hashable
from networkx import DiGraph

from autocnet.fileio import io_json
class CandidateGraph(DiGraph):
    #TODO: This would be better with composition, and then dispatch the 
    # network X calls to the graph object.

    def __init__(self,*args, **kwargs):
        super(CandidateGraph,self).__init__(*args, **kwargs)

    def add_image(self, identifier):
    def add_image(self, identifier, *args, **kwargs):
        """
        Parameters
        ==========
@@ -13,6 +17,36 @@ class CandidateGraph(DiGraph):
                     A Python hashable object to be used as the node key
        """
        if isinstance(identifier, Hashable):
            self.add_node(identifier)
            self.add_node(identifier, *args, **kwargs)
        else:
            raise TypeError('{} is not hashable and can not be a node id'.format(identifier))

    def adjacency_to_json(self, outputfile):
        """
        Write the edge structure to a JSON adjacency list

        Parameters
        ==========
        outputfile : str
                     PATH where the JSON will be written
        """
        adjacency_dict = {}
        for n in self.nodes():
            adjacency_dict[n] = self.neighbors(n)
        io_json.write_json(adjacency_dict, outputfile)

    @classmethod
    def from_adjacency(cls, inputfile):
        """
        Instantiate the class using an adjacency list

        Parameters
        ==========
        inputfile : str
                    The input file containing the graph representation
        """
        #TODO: This is better as a generic reader that tries drivers until 
        # a valid dict is returned.
        adjacency_dict = io_json.read_json(inputfile)
        return cls(adjacency_dict)
Loading