def test_graph_adjacencies(self):
     g = nx.DiGraph()
     g.add_node("a", color="blue", label="a1")
     g.add_nodes_from(["b", "c", "d"])
     g.add_node("root", color="red")
     g.add_edge("root", "a")
     g.add_edge("root", "d")
     g.add_edge("a", "b")
     g.add_edge("a", "c")
     g.add_edge("d", "c")
     res = graph_adjacencies(g)
     exp = [
         {
             "id": "root",
             "name": "root",
             "data": {"color": "red"},
             "adjacencies": [{"nodeTo": "a", "data": {}}, {"nodeTo": "d", "data": {}}],
         },
         {
             "id": "a",
             "name": "a1",
             "data": {"color": "blue"},
             "adjacencies": [{"nodeTo": "b", "data": {}}, {"nodeTo": "c", "data": {}}],
         },
         {"id": "b", "name": "b", "data": {}, "adjacencies": []},
         {"id": "c", "name": "c", "data": {}, "adjacencies": []},
         {"id": "d", "name": "d", "data": {}, "adjacencies": [{"nodeTo": "c", "data": {}}]},
     ]
     self.assertEqual(list(flatten(exp)), list(flatten(res)))
示例#2
0
    def _spacetree(self):
        '''
        '''
        form = FieldStorage(
            self.rfile,
            headers=self.headers,
            environ={'REQUEST_METHOD': 'POST'},
        )

        # Handle uploading of an HDF file:
        file_upload = form['hdf_file']
        if not file_upload.filename:
            self._index(error='Please select a file to upload.')
            return
        # Create a temporary file for the upload:
        file_desc, file_path = mkstemp()
        file_obj = os.fdopen(file_desc, 'w')
        file_obj.write(file_upload.file.read())
        file_obj.close()
        try:
            with hdf_file(file_path) as hdf_file_obj:
                #lfl_params = hdf_file_obj.keys()
                json_dep_tree = json.loads(hdf_file_obj.dependency_tree)
                from networkx.readwrite import json_graph
                gr_st = json_graph.node_link_graph(json_dep_tree)
        except IOError:
            self._index(error='Please select a valid HDF file.')
            return

        # Fetch parameters to display in a grid:
        #self._generate_json(lfl_params)

        # Save the dependency tree to tree.json:
        tree = os.path.join(AJAX_DIR, 'tree.json')
        with open(tree, 'w') as fh:
            json.dump(graph_adjacencies(gr_st), fh, indent=4)

        # Save the list of nodes to node_list.json:
        node_list = os.path.join(AJAX_DIR, 'node_list.json')
        spanning_tree_params = sorted(gr_st.nodes())
        with open(node_list, 'w') as fh:
            json.dump(spanning_tree_params, fh, indent=4)

        #polaris_query, params, missing_lfl_params = self._fetch_params(lfl_params)
        polaris_query, params, missing_lfl_params = '', {}, []

        # Render the spacetree:
        self._respond_with_template(
            'spacetree.html', {
                'missing_lfl_params': missing_lfl_params,
                'params': sorted(params.items()),
                'polaris_query': polaris_query,
                'server': BASE_URL,
                'year': date.today().year,
            })
    def _generate_json(self, lfl_params):
        '''
        Returns list of parameters used in the spanning tree.

        Note: LFL parameters not used will not be returned!
        '''
        print "Establishing Node dependencies from Analysis Engine"
        # Ensure file is a valid HDF file before continuing:
        derived_nodes = get_derived_nodes(settings.NODE_MODULES)
        required_params = derived_nodes.keys()

        # TODO: Update ac_info with keys from provided fields:
        ac_info = {
            'Family': u'B737 NG',
            'Frame': u'737-3C',
            'Identifier': u'15',
            'Main Gear To Lowest Point Of Tail': None,
            'Main Gear To Radio Altimeter Antenna': None,
            'Manufacturer Serial Number': u'39009',
            'Manufacturer': u'Boeing',
            'Model': u'B737-8JP',
            'Precise Positioning': True,
            'Series': u'B737-800',
            'Tail Number': 'G-ABCD',
        }

        # TODO: Option to populate an AFR:
        achieved_flight_record = {}

        # Generate the dependency tree:
        node_mgr = NodeManager(
            {},
            1000,
            lfl_params,
            required_params,
            [],
            derived_nodes,
            ac_info,
            achieved_flight_record,
        )
        _graph = graph_nodes(node_mgr)
        gr_all, gr_st, order = process_order(_graph, node_mgr)

        # Save the dependency tree to tree.json:
        tree = os.path.join(AJAX_DIR, 'tree.json')
        with open(tree, 'w') as fh:
            json.dump(graph_adjacencies(gr_st), fh, indent=4)

        # Save the list of nodes to node_list.json:
        node_list = os.path.join(AJAX_DIR, 'node_list.json')
        spanning_tree_params = sorted(gr_st.nodes())
        with open(node_list, 'w') as fh:
            json.dump(spanning_tree_params, fh, indent=4)
        return
    def _generate_json(self, lfl_params):
        '''
        Returns list of parameters used in the spanning tree.

        Note: LFL parameters not used will not be returned!
        '''
        print "Establishing Node dependencies from Analysis Engine"
        # Ensure file is a valid HDF file before continuing:
        derived_nodes = get_derived_nodes(settings.NODE_MODULES)
        required_params = derived_nodes.keys()

        # TODO: Update ac_info with keys from provided fields:
        ac_info = {
            'Family': u'B737 NG',
            'Frame': u'737-3C',
            'Identifier': u'15',
            'Main Gear To Lowest Point Of Tail': None,
            'Main Gear To Radio Altimeter Antenna': None,
            'Manufacturer Serial Number': u'39009',
            'Manufacturer': u'Boeing',
            'Model': u'B737-8JP',
            'Precise Positioning': True,
            'Series': u'B737-800',
            'Tail Number': 'G-ABCD',
        }

        # TODO: Option to populate an AFR:
        achieved_flight_record = {}

        # Generate the dependency tree:
        node_mgr = NodeManager(
            {},
            1000,
            lfl_params,
            required_params,
            [],
            derived_nodes,
            ac_info,
            achieved_flight_record,
        )
        _graph = graph_nodes(node_mgr)
        gr_all, gr_st, order = process_order(_graph, node_mgr)

        # Save the dependency tree to tree.json:
        tree = os.path.join(AJAX_DIR, 'tree.json')
        with open(tree, 'w') as fh:
            json.dump(graph_adjacencies(gr_st), fh, indent=4)

        # Save the list of nodes to node_list.json:
        node_list = os.path.join(AJAX_DIR, 'node_list.json')
        spanning_tree_params = sorted(gr_st.nodes())
        with open(node_list, 'w') as fh:
            json.dump(spanning_tree_params, fh, indent=4)
        return
 def test_graph_adjacencies(self):
     g = nx.DiGraph()
     g.add_node('a', color='blue', label='a1')
     g.add_nodes_from(['b', 'c', 'd'])
     g.add_node('root', color='red')
     g.add_edge('root', 'a')
     g.add_edge('root', 'd')
     g.add_edge('a', 'b')
     g.add_edge('a', 'c')
     g.add_edge('d', 'c')
     res = graph_adjacencies(g)
     exp = [
         {
             'id':
             'root',
             'name':
             'root',
             'data': {
                 'color': 'red',
             },
             'adjacencies': [
                 {
                     'nodeTo': 'a',
                     'data': {},
                 },
                 {
                     'nodeTo': 'd',
                     'data': {},
                 },
             ],
         },
         {
             'id':
             'a',
             'name':
             'a1',
             'data': {
                 'color': 'blue',
             },
             'adjacencies': [
                 {
                     'nodeTo': 'b',
                     'data': {},
                 },
                 {
                     'nodeTo': 'c',
                     'data': {},
                 },
             ],
         },
         {
             'id': 'b',
             'name': 'b',
             'data': {},
             'adjacencies': [],
         },
         {
             'id': 'c',
             'name': 'c',
             'data': {},
             'adjacencies': [],
         },
         {
             'id': 'd',
             'name': 'd',
             'data': {},
             'adjacencies': [{
                 'nodeTo': 'c',
                 'data': {},
             }],
         },
     ]
     self.assertEqual(list(flatten(exp)), list(flatten(res)))
 def test_graph_adjacencies(self):
     g = nx.DiGraph()
     g.add_node('a', color='blue', label='a1')
     g.add_nodes_from(['b', 'c', 'd'])
     g.add_node('root', color='red')
     g.add_edge('root', 'a')
     g.add_edge('root', 'd')
     g.add_edge('a', 'b')
     g.add_edge('a', 'c')
     g.add_edge('d', 'c')
     res = graph_adjacencies(g)
     exp = [
     {
         'id': 'root',
         'name': 'root',
         'data': {
             'color': 'red',
             },
         'adjacencies': [
             {
                 'nodeTo': 'a',
                 'data': {},
                 },
             {
                 'nodeTo': 'd',
                 'data': {},
                 },
             ],
         },
     {
         'id': 'a',
         'name': 'a1',
         'data': {
             'color': 'blue',
             },
         'adjacencies': [
             {
                 'nodeTo': 'b',
                 'data': {},
                 },
             {
                 'nodeTo': 'c',
                 'data': {},
                 },
             ],
         },
     {
         'id': 'b',
         'name': 'b',
         'data': {},
         'adjacencies': [
             ],
         },
     {
         'id': 'c',
         'name': 'c',
         'data': {},
         'adjacencies': [
             ],
         },
     {
         'id': 'd',
         'name': 'd',
         'data': {},
         'adjacencies': [
             {
                 'nodeTo': 'c',
                 'data': {},
                 }
             ],
         },
     ]
     self.assertEqual(list(flatten(exp)), list(flatten(res)))