<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Session 2
Step1:  Part 2
Step2:  Lists
Lists are the most commonly used data structure. Think of it as a sequence of data that is enclosed in square brackets and data are separated by a comma. Each of these data can be accessed by calling it's index value.
Step3:  In python, Indexing starts from 0. Thus now the list l, which has three elements will have apple at 0 index, orange at 1 index and 123 at 2 index.
Step4:  Tuple
Tuples are similar to lists but only big difference is the elements inside a list can be changed but in tuple it cannot be changed.
Step5:  Values can be assigned while declaring a tuple. It takes a list as input and converts it into a tuple or it takes a string and converts it into a tuple.
Step6:  Dictionaries
Dictionaries are used like set of key-value pairs.
Step7:  Loops
for variable in something
Step8:  Part 3
Step9:  NetworkX with Matplotlib¶
Pros
Step10:  Uploading your network to GraphSpace
First you need to connect to GraphSpace using your username and password.
Step11:  Once you are connected, you can use this connection to post/upload your graphs to GraphSpace.
Step12:  Updating your network on GraphSpace
Step13:  Adding and Inspecting Attributes
Step14:  Adding node labels
label is a text attribute that is displayed inside of the node. GraphSpace uses it to search for nodes with a matching name.
Refer to Node Data Attributes Attributes Treated Specially by GraphSpace for more details.
Step15:  Adding graph information
GraphSpace gives users the freedom to include any attributes such as name, title, description, and tags that best characterize the network.
Refer to Graph Data Attributes to learn more about adding information about the graph.
Step16:  Adding visual styles to nodes
color (str, optional)
Step17:  Adding visual style to edges
edge_style (str, optional)
Step18:  Adding annotations to nodes and edges
'popup' attribute
A string that will be displayed in a popup window when the user clicks the node/edge. This string can be HTML-formatted information, e.g., Gene Ontology annotations and database links for a protein; or types, mechanism, and database sources for an interaction.
Step19:  
Step20:  Laying out nodes
Step21:  Specifying weights on edges
Step22:  Note
Step23:  Part 4
Step24:  Adding and removing group members
Step25:  Sharing graphs with the groups
A user can share one or more graphs with groups to which the user belongs.
Step26:  Unsharing graphs
Step27:  Part 5
Step28:  
Step29:  More examples
Users can also define more advanced style for the graph using attr_dict parameter in add_node_style and add_edge_style methods. The attr_dict is dictionary of style properties supported by Cytoscape.js.
Step30:  Set the layout as default
Step31:  Sharing layouts
Step32:  Similarily you can also get the list of shared layouts using get_shared_graph_layouts method.
Step33:  Part 6
Step34:  Part 7
Step35:  Part 8
Step36:  More examples
Step37:  
Step38:  
<ASSISTANT_TASK:>
Python Code:
!pip install graphspace_python==0.8.3
Explanation: Session 2: Integrating GraphSpace into network analysis projects
Presenters: Aditya Bharadwaj, Jeffrey N. Law and T. M. Murali
Introduction
Required files for today
Clone or download this repository: http://bit.ly/2017icsb
IPython/Jupyter notebooks 
Datasets in the data subdirectory
Required software for today
Both Python 2 and 3 are welcome
Jupyter or IPython (and their dependencies)
Anaconda distribution of Python is an easy way to install these
About Me
PhD Student, CS@VT
Website: adityabharadwaj.in
Email: adb@vt.edu
Twitter: @adbcoder
Agenda
Setup required softwares
Introduction to python programming
Creating and uploading graphs
Basics of NetworkX API
Visualizing networks on GraphSpace
Adding style to the networks
Specifying weights on edges
Laying out nodes programmatically
Managing groups and sharing graphs
Create groups
Add/Remove group members
Share graphs with groups
Managing layouts
Sharing layouts
Set default layout
Publishing graphs
Searching graphs on GraphSpace
RESTful APIs
Exploring and finding the right API
Using RESTful APIs
Part 1. Setup required softwares
Install Jupyter/IPython notebook
The Jupyter Notebook is an interactive computing environment that enables users to author notebook documents that include: - Live code - Interactive widgets - Plots - Narrative text - Equations - Images - Video.
Install Jupyter
Go to Jupyter and follow the instructions.
You can also install Jupyter and Python using the following Anaconda distributions of Python (recommended):
Windows
MacOS
Linux
Start Jupyter
Open command line and go the directory where you installed tutorial repository and start jupyter using the following command:
jupyter notebook
You should see the notebook open in your browser.
Install graphspace-python package
There are multiple ways to install graphspace_python package.
a. Use pip (recommended)
pip install graphspace_python
b. Install manually from PyPi package
https://pypi.python.org/pypi/graphspace_python
c. Install the latest development version from GitHub
git clone https://github.com/adbharadwaj/graphspace-python.git
End of explanation
print("Hello World")
Explanation: Part 2: Introduction to python programming
Python is an interpreted, general-purpose high-level programming language whose design philosophy emphasises code readability
End of explanation
l = []
# l = list()
l = ['apple', 'orange', 123] 
print(l)
Explanation: Lists
Lists are the most commonly used data structure. Think of it as a sequence of data that is enclosed in square brackets and data are separated by a comma. Each of these data can be accessed by calling it's index value.
End of explanation
print(l[0], l[1])
Explanation: In python, Indexing starts from 0. Thus now the list l, which has three elements will have apple at 0 index, orange at 1 index and 123 at 2 index.
End of explanation
tup = ()
#tup = tuple()
Explanation: Tuple
Tuples are similar to lists but only big difference is the elements inside a list can be changed but in tuple it cannot be changed.
End of explanation
tup3 = tuple([1,2,3])
print(tup3)
tup4 = tuple('Hello')
print(tup4)
Explanation: Values can be assigned while declaring a tuple. It takes a list as input and converts it into a tuple or it takes a string and converts it into a tuple.
End of explanation
data = {}
# data = dict()
data['firstname'] = 'Aditya'
data['lastname'] = 'Bharadwaj'
data['age'] = 25
print(data)
Explanation: Dictionaries
Dictionaries are used like set of key-value pairs.
End of explanation
for i in [1,2,3,4,5]:
    print(i)
Explanation: Loops
for variable in something:
    algorithm
End of explanation
import networkx as nx
G = nx.DiGraph()
# Add a node
G.add_node('a')
# Add multiple nodes
G.add_nodes_from(['b', 'c', 'd'])
G.nodes()
# Remove node from the graph
G.remove_node('d')
G.nodes()
# Add edges to the graph
G.add_edge('a', 'b')
G.add_edges_from([('b','c'), ('c', 'a')])
G.edges()
# Remove edge from the graph
G.remove_edge('c', 'a')
G.edges()
# Get Graph Info
print(nx.info(G))
Explanation: Part 3: Creating and uploading graphs
Basic concepts in NetworkX
NetworkX is a Python package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks.
Documentation is available at https://networkx.readthedocs.io/en/stable/
Create an empty graph
End of explanation
%matplotlib inline
nx.draw(G, with_labels=True)
Explanation: NetworkX with Matplotlib¶
Pros:
- Easy
- Some customization
Cons:
- Looks "outdated" (not great for publication / productizing)
- Not interactive
- Few Layout Options
- Offline
End of explanation
from graphspace_python.api.client import GraphSpace
graphspace = GraphSpace('user6@example.com', 'user6')
Explanation: Uploading your network to GraphSpace
First you need to connect to GraphSpace using your username and password.
End of explanation
from graphspace_python.graphs.classes.gsgraph import GSGraph
graph = graphspace.post_graph(GSGraph(G))
print(graph.url)
Explanation: Once you are connected, you can use this connection to post/upload your graphs to GraphSpace.
End of explanation
# Update the name of the graph
graph.set_name('My First Graph')
graph = graphspace.update_graph(graph)
print(graph.url)
Explanation: Updating your network on GraphSpace
End of explanation
G = nx.DiGraph()
# add at creation
# nodes
G.add_node('a', favorite_color='yellow')
G.add_nodes_from([('b', {'favorite_color' : 'green'}),
                  ('c', {'favorite_color' :'red'})])
# edges
G.add_edge('a', 'b', {'relationship' : 'friends'})
G.add_edge('b', 'c', {'relationship' : 'enemy'})
# accessing node attributes
print("Node 'a' attributes:", G.node['a'])
# accessing edge attributes
print("Edge a-b attributes:", G.edge['a']['b'])
Explanation: Adding and Inspecting Attributes
End of explanation
label = {
    'a' : 'A',
    'b' : 'B',
    'c' : 'C'
}
nx.set_node_attributes(G, 'label', label)
print("Node a's label is %s" % G.node['a']['label'])
graph = graphspace.update_graph(GSGraph(G), graph_id=graph.id)
print(graph.url)
Explanation: Adding node labels
label is a text attribute that is displayed inside of the node. GraphSpace uses it to search for nodes with a matching name.
Refer to Node Data Attributes Attributes Treated Specially by GraphSpace for more details.
End of explanation
graph.set_name('My First Graph')
# Add tags
graph.set_tags(['icsb2017', 'tutorial'])
# Add any number of attributes that best characterize the network
graph.set_data({
        'author': 'Aditya Bharadwaj',
        'contact_email': 'adb@vt.edu',
        'description': "This graph was posted during ICSB 2017 workshop on GraphSpace"
    })
graph = graphspace.update_graph(graph, graph_id=graph.id)
print(graph.url)
Explanation: Adding graph information
GraphSpace gives users the freedom to include any attributes such as name, title, description, and tags that best characterize the network.
Refer to Graph Data Attributes to learn more about adding information about the graph.
End of explanation
for n in graph.nodes():
    graph.add_node_style(n, shape='rectangle', color=G.node[n]['favorite_color'], width=100, height=100)
graph = graphspace.update_graph(graph)
print(graph.url)
Explanation: Adding visual styles to nodes
color (str, optional): Hexadecimal representation of the color (e.g., #FFFFFF) or color name. Defaults to white.
shape (str, optional): Shape of node. Defaults to 'ellipse'
List of allowed node shapes
ellipse (default)
rectangle
roundrectangle 
triangle
pentagon
hexagon
heptagon
octagon
star
diamond
vee
rhomboid
End of explanation
graph.add_edge_style('a', 'b', directed=False, edge_style='solid', width=10.0, color='blue')
graph.add_edge_style('b', 'c', directed=False, edge_style='dashed', width=10.0, color='red')
    
graph = graphspace.update_graph(graph)
print(graph.url)
Explanation: Adding visual style to edges
edge_style (str, optional): Style of edge. Defaults to 'solid'.
List of allowed edge styles
solid (default)
dotted
dashed
End of explanation
graph.node['a']['popup'] = 'Node A'
graph.node['b']['popup'] = 'Node B'
graph.node['c']['popup'] = 'Node C'
graph['a']['b']['popup'] = 'Friends'
graph['b']['c']['popup'] = 'Enemy'
graph = graphspace.update_graph(graph)
print(graph.url)
Explanation: Adding annotations to nodes and edges
'popup' attribute
A string that will be displayed in a popup window when the user clicks the node/edge. This string can be HTML-formatted information, e.g., Gene Ontology annotations and database links for a protein; or types, mechanism, and database sources for an interaction.
End of explanation
# HTML formated popups
graph['a']['b']['popup'] = '<h3>Edge between A and B</h3> <br/> Relationship: Friends'
graph['b']['c']['popup'] = '<h3>Edge between B and C</h3> <br/> Relationship: Enemy'
graph.node['a']['popup'] = '<h3>Node A</h3> <br/> <img height="250" src="http://images.clipartpanda.com/boy-20clip-20art-blond-boy.png" alt="Image A" />'
graph.node['b']['popup'] = '<h3>Node B</h3> <br/> <img height="250" src="http://images.clipartpanda.com/boy-clipart-birthday-boy.png" alt="Image B" />'
graph.node['c']['popup'] = '<h3>Node C</h3> <br/> <img height="250" src="http://images.clipartpanda.com/boy-clipart-9a67af7554253b6a9b7014c36c348f09.jpg" alt="Image C" />'
graph = graphspace.update_graph(graph)
print(graph.url)
# You can also embed websites in popups as well. This could be useful for embedding sites like genecards.
graph['a']['b']['popup'] = '<h3>Edge between A and B</h3> <br/> Relationship: <a href="https://en.m.wikipedia.org/wiki/Friendship">Friends</a>  <iframe src="https://en.m.wikipedia.org/wiki/Friendship" scrolling="auto" width="100%" height="500"></iframe>'
graph['b']['c']['popup'] = '<h3>Edge between B and C</h3> <br/> Relationship: <a href="https://en.m.wikipedia.org/wiki/Enemy">Enemy</a>  <iframe src="https://en.m.wikipedia.org/wiki/Enemy" scrolling="auto" width="100%" height="500"></iframe>'
graph = graphspace.update_graph(graph)
print(graph.url)
Explanation: 
End of explanation
# Position nodes in a vertical alignment.
graph.set_node_position('a', y=0, x=0)
graph.set_node_position('b', y=250, x=0)
graph.set_node_position('c', y=500, x=0)
graph = graphspace.update_graph(graph)
print(graph.url)
Explanation: Laying out nodes
End of explanation
# G.add_edge('a', 'b', weight=5)
# G.add_edge('b', 'c', weight=10)
graph['a']['b']['weight']  = 5
graph['b']['c']['weight']  = 10
graph.edges(data=True)
graph = graphspace.update_graph(graph, graph_id=graph.id)
print(graph.url)
Explanation: Specifying weights on edges
End of explanation
# Laying out edges with width propotional to edge weight.
graph.add_edge_style('a', 'b', directed=False, edge_style='solid', width=graph['a']['b']['weight'], color='blue')
graph.add_edge_style('b', 'c', directed=False, edge_style='dashed', width=graph['b']['c']['weight'], color='red')
graph = graphspace.update_graph(graph)
print(graph.url)
Explanation: Note: If you add weights as a edge property, it will not reflect on graph visualization automatically.
End of explanation
from graphspace_python.graphs.classes.gsgroup import GSGroup
group = graphspace.post_group(GSGroup(name='My first group', description='sample group'))
print(group.url)
Explanation: Part 4: Managing collaborative groups and sharing graphs
A group is a collection of GraphSpace users. For example, if there are multiple researchers who are collaborating a project, a group may be created containing all of them. 
A group owner is the creator of the group. Any GraphSpace user can create a group by visiting the Groups page and clicking the “Create group” button. The group owner may
Invite any GraphSpace user that has an account to be a member of their group.
Remove any member from the group.
Unshare any graph that has already been shared by the members of the group
A group member is a user who is a part of a group. (A group owner is trivially a member of the group.) A group member may
Share a graph owned by him or her with a group.
Unshare a previously shared graph.
Share a layout for a previously shared graph.
Unshare a previously shared layout.
Creating groups
End of explanation
# Initially a group is created with the group owner as a member.
for member in graphspace.get_group_members(group=group):
    print(member.email)
# Group owner can add existing users by their GraphSpace usernames
for email in ['adb@vt.edu', 'adb@cs.vt.edu']:
    graphspace.add_group_member(group=group, member_email=email)
# Getting the list of all group members
for member in graphspace.get_group_members(group=group):
    print(member.email, member.id)
# Send the following invitation link to your collaborators (with/without GraphSpace accounts)
print(group.invite_link)
graphspace.delete_group_member(group=group, member_id=70)
for member in graphspace.get_group_members(group=group):
    print(member.email)
Explanation: Adding and removing group members
End of explanation
graphspace.share_graph(graph=graph, group=group)
for shared_graph in graphspace.get_group_graphs(group=group):
    print(shared_graph.owner_email, shared_graph.name)
Explanation: Sharing graphs with the groups
A user can share one or more graphs with groups to which the user belongs.
End of explanation
graphspace.unshare_graph(graph=graph, group=group)
# No graph is shared with the group at this point.
for shared_graph in graphspace.get_group_graphs(group=group):
    print(shared_graph.owner_email, shared_graph.name)
Explanation: Unsharing graphs
End of explanation
from graphspace_python.graphs.classes.gslayout import GSLayout
L = GSLayout()
# Assign different colors to nodes
L.add_node_style('a', shape='ellipse', color='yellow', width=100, height=100)
L.add_node_style('b', shape='triangle', color='green', width=100, height=100)
L.add_node_style('c', shape='rectangle', color='red', width=100, height=100)
L.add_edge_style('a', 'b', directed=False, edge_style='solid', width=5.0, color='blue')
L.add_edge_style('b', 'c', directed=False, edge_style='dashed', width=5.0, color='red')
L.set_name('My First Layout')
layout = graphspace.post_graph_layout(graph_id=graph.id, layout=L)
# Go to the following url o visualize the layout.
print(layout.url)
Explanation: Part 5: Managing layouts
Creating and uploading layouts
End of explanation
layout.set_node_position('a', y=0, x=0)
layout.set_node_position('b', y=0, x=250)
layout.set_node_position('c', y=0, x=500)
layout = graphspace.update_graph_layout(graph_id=graph.id, layout=layout)
# Go to the following url to visualize the layout.
print(layout.url)
Explanation: 
End of explanation
# Setting font style
layout.add_node_style('a', attr_dict={'font-size':24, 'font-family': 'Lucida Console, Courier, monospace'}, shape='ellipse', color=graph.node['a']['favorite_color'], width=100, height=100)
layout.add_node_style('b', attr_dict={'font-size':24, 'font-family': 'Lucida Console, Courier, monospace'}, shape='triangle', color=graph.node['b']['favorite_color'], width=100, height=100)
layout.add_node_style('c', attr_dict={'font-size':24, 'font-family': 'Lucida Console, Courier, monospace'}, shape='rectangle', color=graph.node['c']['favorite_color'], width=100, height=100)
layout = graphspace.update_graph_layout(graph_id=graph.id, layout=layout)
print(layout.url)
# Setting image backgrounds
import pickle
tutorial_node_images = pickle.load( open( "data/tutorial_node_images.p", "rb" ) )
layout.add_node_style('a', attr_dict={
    'background-image': tutorial_node_images['a'],
    'background-clip': 'none',
    'background-fit': 'contain',
    'background-opacity': 0,
    'border-opacity': 0,
    'text-margin-y': 5
}, width=100, height=100)
layout.add_node_style('b', attr_dict={
    'background-image': tutorial_node_images['b'],
    'background-clip': 'none',
    'background-fit': 'contain',
    'background-opacity': 0,
    'border-opacity': 0,
    'text-margin-y': 5
}, width=100, height=100)
layout.add_node_style('c', attr_dict={
    'background-image': tutorial_node_images['c'],
    'background-clip': 'none',
    'background-fit': 'contain',
    'background-opacity': 0,
    'border-opacity': 0,
    'text-margin-y': 5
}, width=100, height=100)
layout = graphspace.update_graph_layout(graph_id=graph.id, layout=layout)
print(layout.url)
Explanation: More examples
Users can also define more advanced style for the graph using attr_dict parameter in add_node_style and add_edge_style methods. The attr_dict is dictionary of style properties supported by Cytoscape.js.
End of explanation
graph = graphspace.set_default_graph_layout(graph=graph, layout=layout)
# You should see the above layout by default for the given graph.
print(graph.url)
Explanation: Set the layout as default
End of explanation
for mylayout in graphspace.get_my_graph_layouts(graph_id=graph.id):
    print(mylayout.name, mylayout.id)
Explanation: Sharing layouts
End of explanation
layouts = graphspace.get_shared_graph_layouts(graph=graph)
print(layouts)
layout.set_is_shared(1)
layout = graphspace.update_graph_layout(graph=graph, layout=layout)
print([l.name for l in graphspace.get_shared_graph_layouts(graph=graph)])
Explanation: Similarily you can also get the list of shared layouts using get_shared_graph_layouts method.
End of explanation
# Sharing the graph with everyone. This graph will show up in Public Graphs list.
graph = graphspace.publish_graph(graph=graph)
print(graph.url)
# Unpublishing graphs
graph = graphspace.unpublish_graph(graph_id=graph.id)
print(graph.url)
Explanation: Part 6: Publishing graphs
End of explanation
# Getting a list of public graphs with 'pathlinker' as a subtring in atleast one of their tags.
for g in graphspace.get_public_graphs(tags=['%pathlinker%'],limit=100, offset=0):
    print(g.owner_email, g.name)
# Getting a list of my graphs.
for g in graphspace.get_my_graphs(limit=100, offset=0):
    print(g.owner_email, g.name)
Explanation: Part 7: Searching graphs on GraphSpace
You can search for graphs based on their visibility.
- Graphs posted by you
- Graphs shared with one of your groups
- Graphs shared with everyone
End of explanation
graphspace.delete_group(group=group)
graphspace.delete_graph(graph=graph)
Explanation: Part 8: RESTful APIs
The GraphSpace REST API provides endpoints for entities such as graphs, layouts, and groups that allow developers to interact with the GraphSpace website remotely by sending and receiving JSON objects. This API enables developers to create, read, and update GraphSpace content from client-side JavaScript or from applications written in any language.
Finding the right API for you
API Reference
Testing GraphSpace APIs
Postman is a Google Chrome app for interacting with HTTP APIs. It provides a friendly GUI for constructing requests and reading responses. Postman makes it easy to test, develop and document APIs by allowing users to quickly put together both simple and complex HTTP requests.
Postman Installation
Postman is available as a native app (recommended) for Mac / Windows / Linux, and as a Chrome App. The Postman Chrome app can only run on the Chrome browser. To use the Postman Chrome app, you need to:
Install Google Chrome: Install Chrome.
If you already have Chrome installed, head over to Postman’s page on the Chrome Webstore – https://chrome.google.com/webstore/detail/postman-rest-client-packa/fhbjgbiflinjbdggehcddcbncdddomop?hl=en, and click ‘Add to Chrome’.
After the download is complete, launch the app.
Download Postman Collection
Importing the postman collection:
Click Import button in the top menu.
Choose the Import File in the pop up window.
Provide the Authorization details for the imported requests (as Authorization details have been removed for security concern)
Deleting groups,graphs and layouts posted during this tutorial
End of explanation
## Map values to node color
g = nx.erdos_renyi_graph(10, 0.1, seed=10, directed=True)
g = GSGraph(nx.relabel_nodes(g, {n: str(n) for n in g.nodes()}))
nx.set_node_attributes(g, 'degree_centrality', nx.degree_centrality(g))
for n in g.nodes():
    g.add_node_style(n, attr_dict={'background-opacity': g.node[n]['degree_centrality']}, color='blue', width=50, height=50)
g.set_name('Random Graph with degree centrality mapped to node color')
graph = graphspace.post_graph(g)
print(graph.url)
Explanation: More examples
End of explanation
graphspace.delete_graph(graph=graph)
## Map values to node size
g = nx.erdos_renyi_graph(10, 0.1, seed=10, directed=True)
g = GSGraph(nx.relabel_nodes(g, {n: str(n) for n in g.nodes()}))
nx.set_node_attributes(g, 'degree_centrality', nx.degree_centrality(g))
for n in g.nodes():
    g.add_node_style(n, color='blue', width=200*g.node[n]['degree_centrality']+20, height=200*g.node[n]['degree_centrality']+20)
g.set_name('Random Graph with degree centrality mapped to node size')
graph = graphspace.post_graph(g)
print(graph.url)
Explanation: 
End of explanation
graphspace.delete_graph(graph=graph)
Explanation: 
End of explanation
<END_TASK>