Ejemplo n.º 1
0
    def test_invalid_columns(self):
        sifFilename = tempfile.NamedTemporaryFile(suffix='.sif', delete=False)
        try:
            # Write the network file
            write_valid_graph(sifFilename)
            # Add another line with an extra column
            sifFilename.write('A\tdd\tF\t0.85\n')
        finally:
            sifFilename.close()

        # Load the DiGraph and expect an Exception for the extra column
        with pytest.raises(Exception):
            loadGraph(sifFilename.name)

        # Remove because delete=False above
        os.remove(sifFilename.name)
Ejemplo n.º 2
0
    def test_valid_graph(self):
        sifFilename = tempfile.NamedTemporaryFile(suffix='.sif', delete=False)
        try:
            # Write the network file
            write_valid_graph(sifFilename)
        finally:
            sifFilename.close()

        # Load the DiGraph
        graph = loadGraph(sifFilename.name)

        # Check that the DiGraph has the expected properties
        assert graph.order() == 5, "Unexpected number of nodes"
        assert graph.size() == 7, "Unexpected number of edges"

        # Check that the DiGraph has the expected edges
        assert graph.has_edge('A', 'B')
        assert graph.has_edge('B', 'A')
        assert graph.has_edge('B', 'C')
        assert graph.has_edge('C', 'D')
        assert graph.has_edge('D', 'C')
        assert graph.has_edge('A', 'E')
        assert graph.has_edge('E', 'A')

        # Remove because delete=False above
        os.remove(sifFilename.name)
Ejemplo n.º 3
0
 def test_invalid_columns(self):
     sifFilename= tempfile.NamedTemporaryFile(suffix='.sif', delete=False)
     try:
         # Write the network file
         write_valid_graph(sifFilename)
         # Add another line with an extra column
         sifFilename.write('A\tdd\tF\t0.85\n')
     finally:
         sifFilename.close()
         
     # Load the DiGraph and expect an Exception for the extra column
     with pytest.raises(Exception):
         loadGraph(sifFilename.name)
             
     # Remove because delete=False above
     os.remove(sifFilename.name)
Ejemplo n.º 4
0
 def test_valid_graph(self):
     sifFilename= tempfile.NamedTemporaryFile(suffix='.sif', delete=False)
     try:
         # Write the network file
         write_valid_graph(sifFilename)
     finally:
         sifFilename.close()
         
     # Load the DiGraph
     graph = loadGraph(sifFilename.name)
     
     # Check that the DiGraph has the expected properties
     assert graph.order() == 5, "Unexpected number of nodes"
     assert graph.size() == 7, "Unexpected number of edges"
     
     # Check that the DiGraph has the expected edges
     assert graph.has_edge('A','B')
     assert graph.has_edge('B','A')
     assert graph.has_edge('B','C')
     assert graph.has_edge('C','D')
     assert graph.has_edge('D','C')
     assert graph.has_edge('A','E')
     assert graph.has_edge('E','A')
     
     # Remove because delete=False above
     os.remove(sifFilename.name)
Ejemplo n.º 5
0
def run_forest(msgsteiner, conf_params, forest_opts):
    '''
    Run Forest on the test network
    
    INPUT:
    msgsteiner - the path to msgsteiner
    conf_params - dictionary, see write_conf
    forest_opts - dictionary with long form option names for forest.py as keys

    OUTPUT:
    graph - the DiGraph object for the optimal Steiner forest
    objective - the objective function value of the optimal Steiner forest (float)
    '''
    assert msgsteiner is not None, 'Please provide path to msgsteiner using --msgpath option'
    forest_opts = copy.deepcopy(forest_opts)
    forest_opts['msgpath'] = msgsteiner

    # For reproducibility, though it is likely not needed for this
    # small test case        
    forest_opts['seed'] = 2016
    
    # Write the configuration file with the specified parameters
    conf_filename= tempfile.NamedTemporaryFile(suffix='.txt', delete=False)
    try:
        write_conf(conf_filename, conf_params)
    finally:
        conf_filename.close()
    forest_opts['conf'] = conf_filename.name
    
    default_outpath = True
    if 'outpath' in forest_opts:
      default_outpath = False
    else:
      # Create a tmp directory for output unless one is provided
      forest_opts['outpath'] = tempfile.mkdtemp()
    
    try:
        cur_dir = os.path.dirname(__file__)
        forest_opts['forest'] = os.path.join(cur_dir, '..', 'scripts', 'forest.py')
        forest_cmd = 'python {forest} --prize={prize} --edge={edge} --conf={conf} --dummyMode={dummyMode} --outpath={outpath} --msgpath={msgpath} --seed={seed}'.format(**forest_opts)
        subprocess.call(shlex.split(forest_cmd), shell=False)	

        # Test the optimal Forest to see if parameter value has the intended effect
        opt_forest = os.path.join(forest_opts['outpath'], 'result_optimalForest.sif')
        assert os.path.isfile(opt_forest), 'Forest did not generate the optimal forest file'
        graph = loadGraph(opt_forest)
        
        # Parse the objective function value of the optimal forest
        # This cannot be recovered from the forest file because it depends on w
        info_filename = os.path.join(forest_opts['outpath'], 'result_info.txt')
        assert os.path.isfile(info_filename), 'Forest did not generate the info file'
        objective = parse_obj(info_filename)
        
    finally:
        # Remove here because delete=False above
        os.remove(conf_filename.name)
        # Remove the Forest output directory and all files
        # Leave the output directory intact if it was manually set
        if default_outpath:
          shutil.rmtree(forest_opts['outpath'])
        
    return (graph, objective)