Networkx - Subgraphs using node attributes












0















I have a set of data where the nodes have an attribute showing the name of the team to which they belong. I would like to work out the stats for a) the whole network and then b) the stats for each team comparing their connectivity etc. (Lets say I am comparing who sends emails to who and therefore the degree of connectedness of the team. Here is my sample code:



ST = nx.Graph()
ST.add_node('A',role = 'sales1')
ST.add_node('B',role = 'sales1')
ST.add_node('C',role = 'sales1')
ST.add_node('D',role = 'sales1')
ST.add_node('E',role = 'sales2')
ST.add_node('F',role = 'sales2')
ST.add_node('G',role = 'sales2')
ST.add_node('H',role = 'sales2')

ST.add_edges_from([('A','B'),
('A','C'),
('A','D'),
('B','D'),
('B','C'),
('C','D'),
('E','F'),
('E','G'),
('E','H'),
('F','H'),
('G','H'),
('A','E'),
('G','D')])


I know I can form a subgraph as follows:



H = ST.subgraph(['A','B','C','D'])


Is there an easier way of setting the nodes by reference to the node attribute?










share|improve this question



























    0















    I have a set of data where the nodes have an attribute showing the name of the team to which they belong. I would like to work out the stats for a) the whole network and then b) the stats for each team comparing their connectivity etc. (Lets say I am comparing who sends emails to who and therefore the degree of connectedness of the team. Here is my sample code:



    ST = nx.Graph()
    ST.add_node('A',role = 'sales1')
    ST.add_node('B',role = 'sales1')
    ST.add_node('C',role = 'sales1')
    ST.add_node('D',role = 'sales1')
    ST.add_node('E',role = 'sales2')
    ST.add_node('F',role = 'sales2')
    ST.add_node('G',role = 'sales2')
    ST.add_node('H',role = 'sales2')

    ST.add_edges_from([('A','B'),
    ('A','C'),
    ('A','D'),
    ('B','D'),
    ('B','C'),
    ('C','D'),
    ('E','F'),
    ('E','G'),
    ('E','H'),
    ('F','H'),
    ('G','H'),
    ('A','E'),
    ('G','D')])


    I know I can form a subgraph as follows:



    H = ST.subgraph(['A','B','C','D'])


    Is there an easier way of setting the nodes by reference to the node attribute?










    share|improve this question

























      0












      0








      0








      I have a set of data where the nodes have an attribute showing the name of the team to which they belong. I would like to work out the stats for a) the whole network and then b) the stats for each team comparing their connectivity etc. (Lets say I am comparing who sends emails to who and therefore the degree of connectedness of the team. Here is my sample code:



      ST = nx.Graph()
      ST.add_node('A',role = 'sales1')
      ST.add_node('B',role = 'sales1')
      ST.add_node('C',role = 'sales1')
      ST.add_node('D',role = 'sales1')
      ST.add_node('E',role = 'sales2')
      ST.add_node('F',role = 'sales2')
      ST.add_node('G',role = 'sales2')
      ST.add_node('H',role = 'sales2')

      ST.add_edges_from([('A','B'),
      ('A','C'),
      ('A','D'),
      ('B','D'),
      ('B','C'),
      ('C','D'),
      ('E','F'),
      ('E','G'),
      ('E','H'),
      ('F','H'),
      ('G','H'),
      ('A','E'),
      ('G','D')])


      I know I can form a subgraph as follows:



      H = ST.subgraph(['A','B','C','D'])


      Is there an easier way of setting the nodes by reference to the node attribute?










      share|improve this question














      I have a set of data where the nodes have an attribute showing the name of the team to which they belong. I would like to work out the stats for a) the whole network and then b) the stats for each team comparing their connectivity etc. (Lets say I am comparing who sends emails to who and therefore the degree of connectedness of the team. Here is my sample code:



      ST = nx.Graph()
      ST.add_node('A',role = 'sales1')
      ST.add_node('B',role = 'sales1')
      ST.add_node('C',role = 'sales1')
      ST.add_node('D',role = 'sales1')
      ST.add_node('E',role = 'sales2')
      ST.add_node('F',role = 'sales2')
      ST.add_node('G',role = 'sales2')
      ST.add_node('H',role = 'sales2')

      ST.add_edges_from([('A','B'),
      ('A','C'),
      ('A','D'),
      ('B','D'),
      ('B','C'),
      ('C','D'),
      ('E','F'),
      ('E','G'),
      ('E','H'),
      ('F','H'),
      ('G','H'),
      ('A','E'),
      ('G','D')])


      I know I can form a subgraph as follows:



      H = ST.subgraph(['A','B','C','D'])


      Is there an easier way of setting the nodes by reference to the node attribute?







      python networkx






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 12:25









      RobHRobH

      32




      32
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You can use itertools.groupby:



          from itertools import groupby

          # Sort the nodes by their role (this is required for the groupby to work)
          sorted_by_role = sorted(ST.nodes(data=True), key=lambda node_data: node_data[1]["role"])
          # Group objects with same role together
          grouped = groupby(sorted_by_role, key=lambda node_data: node_data[1]["role"])

          subgraphs = dict()
          for key, group in grouped:
          nodes_in_group, _ = zip(*list(group)) # We don't care about the data here, only the node names
          subgraphs[key] = ST.subgraph(nodes_in_group)


          Now the subgraphs dict contains the subgraph of every different role present in the graph. This works for an arbitrary number of roles.






          share|improve this answer
























          • thank you. That works a treat!

            – RobH
            Nov 15 '18 at 14:13











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319485%2fnetworkx-subgraphs-using-node-attributes%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You can use itertools.groupby:



          from itertools import groupby

          # Sort the nodes by their role (this is required for the groupby to work)
          sorted_by_role = sorted(ST.nodes(data=True), key=lambda node_data: node_data[1]["role"])
          # Group objects with same role together
          grouped = groupby(sorted_by_role, key=lambda node_data: node_data[1]["role"])

          subgraphs = dict()
          for key, group in grouped:
          nodes_in_group, _ = zip(*list(group)) # We don't care about the data here, only the node names
          subgraphs[key] = ST.subgraph(nodes_in_group)


          Now the subgraphs dict contains the subgraph of every different role present in the graph. This works for an arbitrary number of roles.






          share|improve this answer
























          • thank you. That works a treat!

            – RobH
            Nov 15 '18 at 14:13
















          0














          You can use itertools.groupby:



          from itertools import groupby

          # Sort the nodes by their role (this is required for the groupby to work)
          sorted_by_role = sorted(ST.nodes(data=True), key=lambda node_data: node_data[1]["role"])
          # Group objects with same role together
          grouped = groupby(sorted_by_role, key=lambda node_data: node_data[1]["role"])

          subgraphs = dict()
          for key, group in grouped:
          nodes_in_group, _ = zip(*list(group)) # We don't care about the data here, only the node names
          subgraphs[key] = ST.subgraph(nodes_in_group)


          Now the subgraphs dict contains the subgraph of every different role present in the graph. This works for an arbitrary number of roles.






          share|improve this answer
























          • thank you. That works a treat!

            – RobH
            Nov 15 '18 at 14:13














          0












          0








          0







          You can use itertools.groupby:



          from itertools import groupby

          # Sort the nodes by their role (this is required for the groupby to work)
          sorted_by_role = sorted(ST.nodes(data=True), key=lambda node_data: node_data[1]["role"])
          # Group objects with same role together
          grouped = groupby(sorted_by_role, key=lambda node_data: node_data[1]["role"])

          subgraphs = dict()
          for key, group in grouped:
          nodes_in_group, _ = zip(*list(group)) # We don't care about the data here, only the node names
          subgraphs[key] = ST.subgraph(nodes_in_group)


          Now the subgraphs dict contains the subgraph of every different role present in the graph. This works for an arbitrary number of roles.






          share|improve this answer













          You can use itertools.groupby:



          from itertools import groupby

          # Sort the nodes by their role (this is required for the groupby to work)
          sorted_by_role = sorted(ST.nodes(data=True), key=lambda node_data: node_data[1]["role"])
          # Group objects with same role together
          grouped = groupby(sorted_by_role, key=lambda node_data: node_data[1]["role"])

          subgraphs = dict()
          for key, group in grouped:
          nodes_in_group, _ = zip(*list(group)) # We don't care about the data here, only the node names
          subgraphs[key] = ST.subgraph(nodes_in_group)


          Now the subgraphs dict contains the subgraph of every different role present in the graph. This works for an arbitrary number of roles.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 12:43









          RunOrVeithRunOrVeith

          1,1971223




          1,1971223













          • thank you. That works a treat!

            – RobH
            Nov 15 '18 at 14:13



















          • thank you. That works a treat!

            – RobH
            Nov 15 '18 at 14:13

















          thank you. That works a treat!

          – RobH
          Nov 15 '18 at 14:13





          thank you. That works a treat!

          – RobH
          Nov 15 '18 at 14:13




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319485%2fnetworkx-subgraphs-using-node-attributes%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python