Skip to content

Commit

Permalink
improving Networks methods
Browse files Browse the repository at this point in the history
  • Loading branch information
J-A-Ha committed Sep 19, 2024
1 parent ccf2845 commit 6da8bca
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
31 changes: 21 additions & 10 deletions art/classes/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ def __init__(self, graph = None, n = 0, edges = None, directed = False, graph_at

def __repr__(self):

"""
Defines how Network objects are represented in string form.
"""

if self.is_directed() == True:
dir = 'Directed'
else:
Expand Down Expand Up @@ -155,38 +159,45 @@ def __repr__(self):
return output

def is_weighted(self) -> bool:

"""
Checks whether edges have a 'weight' attribute. Returns True if yes; False if no.
"""

return 'weight' in self.es.attributes()

def degrees_dataframe(self, direction = 'all'):
def degrees_dataframe(self):

"""
Returns the network's degree distribution as a dataframe.
Returns the network's degree distributions as a dataframe.
"""

degrees_dataframe = pd.DataFrame(columns = ['vertex', 'degree'])
degrees = Network.degree(self, mode = direction)
degrees_dataframe = pd.DataFrame(columns = ['vertex', 'total_degree', 'in_degree', 'out_degree'])
total_degrees = Network.degree(self, mode = 'all')
in_degrees = Network.degree(self, mode = 'in')
out_degrees = Network.degree(self, mode = 'out')

index = 0
for item in self.vs['name']:
degrees_dataframe.loc[index] = [item, degrees[index]]
degrees_dataframe.loc[index] = [item, total_degrees[index], in_degrees[index], out_degrees[index]]
index += 1

degrees_dataframe.index.name = 'vertex_id'
degrees_dataframe = degrees_dataframe.sort_values('degree', ascending=False)
degrees_dataframe = degrees_dataframe.sort_values('total_degree', ascending=False)

return degrees_dataframe


def degree_stats(self, direction = 'all'):
def degree_stats(self):

"""
Returns frequency statistics for the network's degree distribution.
Returns frequency statistics for the network's degree distributions.
"""

df = self.degrees_dataframe(direction = direction)
df = self.degrees_dataframe()

if df is not None:
return df['degree'].describe()
return df['total_degree'].describe()
else:
return None

Expand Down
1 change: 0 additions & 1 deletion art/exporters/network_exporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from typing import List, Dict, Tuple
import json
import copy
import igraph as ig # type: ignore
from igraph import Graph # type: ignore
from networkx.classes import Graph as NetworkX_Undir, DiGraph as NetworkX_Dir, MultiGraph as NetworkX_Multi # type: ignore
Expand Down

0 comments on commit 6da8bca

Please sign in to comment.