Commit b4c6c4c5 authored by dirk.wintergruen's avatar dirk.wintergruen
Browse files

added filter_es

parent ecb235af
__version__ = "0.9.3.9"
__version__ = "0.9.4.0"
......@@ -234,6 +234,16 @@ class MultiLayerGraph(OrderedDict):
An extended dictionary which contains a MultiLayerGraphObject for each year.
"""
def __init__(self):
self._start = None
self._end = None
self._startyear_attr = None
self._endyear_attr = None
self._startyear_edge_attr = None
self._endyear_edge_attr = None
super().__init__()
def init_params(self, start, end, startyear_attr, endyear_attr, startyear_edge_attr, endyear_edge_attr):
self._start = start
self._end = end
......@@ -263,7 +273,30 @@ class MultiLayerGraph(OrderedDict):
update=True
)
def filter_es(self, *args, **kwds):
"""filter all nodes given bei args,kwargs, parameters are the same as in igraph.es.select
(#TODO still experimental !!)
:param args: same as for igraph.vs.select
:param kwds: same as for igraph.vs.select
:return: MultiLayerGraph without the selected nodes.
"""
return self._filter("edge",*args,**kwds)
def filter_vs(self, *args, **kwds):
"""filter all nodes given bei args,kwargs, parameters are the same as in igraph.es.select
(#TODO still experimental !!)
:param args: same as for igraph.vs.select
:param kwds: same as for igraph.vs.select
:return: MultiLayerGraph without the selected nodes.
"""
return self._filter("vertex", *args, **kwds)
def _filter(self, vertex_or_edge, *args, **kwds):
"""filter all nodes given bei args,kwargs, parameters are the same as in igraph.vs.select
(#TODO still experimental !!)
:param args: same as for igraph.vs.select
......@@ -272,6 +305,8 @@ class MultiLayerGraph(OrderedDict):
:return: MultiLayerGraph without the selected nodes.
"""
assert vertex_or_edge in ["edge","vertex"], "first parameter has to be 'edge' or 'vertex'"
if "transform" in kwds:
transform_func = kwds["transform"]
del kwds["transform"]
......@@ -279,7 +314,6 @@ class MultiLayerGraph(OrderedDict):
else:
transform_func = None
logger.debug("Step 2: ynws")
nw = self.copy()
for y, ynw in self.items():
......@@ -287,23 +321,36 @@ class MultiLayerGraph(OrderedDict):
if isinstance(ynw,MultiLayerGraph):
try:
if transform_func:
transform(ynw.multi_layer, transform_func)
transform(ynw.multi_layer, transform_func, vertex_or_edge = vertex_or_edge)
except NotCalculatedYetError:
pass
nds = ynw.multi_layer.vs.select(*args, **kwds)
if vertex_or_edge == "vertex":
nds = ynw.multi_layer.vs.select(*args, **kwds)
else:
nds = ynw.multi_layer.es.select(*args, **kwds)
nds.delete()
for v, k in ynw.items():
if transform_func:
transform(k, transform_func)
nds = k.vs.select(*args, **kwds)
nds.delete()
transform(k, transform_func, vertex_or_edge = vertex_or_edge)
try:
if vertex_or_edge == "vertex":
nds = k.vs.select(*args, **kwds)
else:
nds = k.es.select(*args, **kwds)
nds.delete()
except:
e = sys.exc_info()
logger.debug(e[0], e[1], e[2])
logger.warning("""Something (%s) went wrong in year %s.
Most likely one of the attributes doesn't exit in this year.""" % (e[0], y))
else:
try:
if transform_func:
transform(ynw, transform_func)
transform(ynw, transform_func,vertex_or_edge = vertex_or_edge)
nds = ynw.vs.select(*args, **kwds)
nds.delete()
......@@ -1735,8 +1782,28 @@ class MultiLayerGraph(OrderedDict):
return (sims, ynws_lc), graphproperties
def filter_es(self, *args, **kwds):
"""filter all nodes given bei args,kwargs, parameters are the same as in igraph.es.select
(#TODO still experimental !!)
:param args: same as for igraph.vs.select
:param kwds: same as for igraph.vs.select
:return: MultiLayerGraph without the selected nodes.
"""
def filter_vs(self,*args, **kwds):
return self._filter("edge", *args, **kwds)
def filter_vs(self, *args, **kwds):
"""filter all nodes given bei args,kwargs, parameters are the same as in igraph.es.select
(#TODO still experimental !!)
:param args: same as for igraph.vs.select
:param kwds: same as for igraph.vs.select
:return: MultiLayerGraph without the selected nodes.
"""
return self._filter("vertex", *args, **kwds)
def _filter(self,vertex_or_edge,*args, **kwds):
"""filter all nodes given bei args,kwargs, parameters are the same as in igraph.vs.select
(#TODO still experimental !!)
:param args: same as for igraph.vs.select
......@@ -1747,8 +1814,9 @@ class MultiLayerGraph(OrderedDict):
nw = super().copy()
# now the layers of each year network
logger.debug("Step 2: ynws")
nw._ynws = self._ynws.filter_vs(*args, **kwds)
logger.debug("Step 1: ynws")
nw._ynws = self._ynws._filter(vertex_or_edge,*args, **kwds)
# nw._ynws = self._ynws.copy()
# for y, ynw in nw._ynws.items():
#
......@@ -1778,9 +1846,12 @@ class MultiLayerGraph(OrderedDict):
for v, k in nw.items():
if transform_func:
transform(k,transform_func)
transform(k,transform_func,vertex_or_edge = vertex_or_edge)
nds = k.vs.select(*args, **kwds)
if vertex_or_edge == "vertex":
nds = k.vs.select(*args, **kwds)
else:
nds = k.es.select(*args, **kwds)
nds.delete()
logger.debug("Step 3: merged_ynws")
......@@ -1792,26 +1863,37 @@ class MultiLayerGraph(OrderedDict):
for intervall, merged_ynws in nw._merged_ynws.items():
for y, ynw in merged_ynws.ynws.items():
if transform_func:
transform(ynw.multi_layer,transform_func)
transform(ynw.multi_layer,transform_func,vertex_or_edge = vertex_or_edge)
nds = ynw.multi_layer.vs.select(*args, **kwds)
if vertex_or_edge == "vertex":
nds = ynw.multi_layer.vs.select(*args, **kwds)
else:
nds = ynw.multi_layer.es.select(*args, **kwds)
#if len(nds) > 0:
# print("del %s %s"%(y,intervall))
nds.delete()
for v, k in ynw.items():
if transform_func:
transform(k,transform_func)
try:
if transform_func:
transform(k,transform_func,vertex_or_edge=vertex_or_edge)
if vertex_or_edge == "vertex":
nds = k.vs.select(*args, **kwds)
else:
nds = k.es.select(*args, **kwds)
nds.delete()
except:
e = sys.exc_info()
logger.debug(e[0], e[1], e[2])
logger.warning("""Something (%s) went wrong in year %s.
Most likely one of the attributes doesn't exit in this year.""" % (e[0], y))
nds = k.vs.select(*args, **kwds)
nds.delete()
#if len(nds) > 0:
# print("%s %s %s del"% (v,y,intervall))
except NotCalculatedYetError:
pass # if not calculated in source ignore
nw.generateMultiLayer(v_attr_name=self._multi_layer_v_attr_name)
return nw
......@@ -1921,16 +2003,22 @@ def to_undirected(gr):
return gr_new
def transform(gr, transform):
def transform(gr, transform,vertex_or_edge = "vertex"):
"""transformes values and creates new for a graph
:param transform: iterable of dict. Each tripel contains "name" name of vertex attribute to be transformed,
"func" a callable which can be called with the values of "name". "default" a value which should be used if the
function call doesn't work. (e.g. {"name" : year, "func" : int, "default": 0})
"""
assert vertex_or_edge in ["vertex", "edge"], "vertex_or_edge has to be either 'vertex' or 'edge'"
for t in transform:
new_val = []
if vertex_or_edge == "vertex":
iterate = gr.vs[t["name"]]
else:
iterate = gr.es[t["name"]]
for val in gr.vs[t["name"]]:
try:
new_val.append(t["func"](val))
......
......@@ -29,6 +29,31 @@ class TestMultiLayerGraph(TestCase):
bip = MultiLayerGraph.BiPartiteGraph.load("data/person_nationality.graphml",format="graphml")
def test_filter__es_ynw(self):
test_title = "GRAVITATIONAL-SCALAR FIELD COUPLING"
pt = "data/co-author-social_years_simplified_3.tgz"
with open(pt, "rb") as outf:
ynw_load = igraphx.loadGraphsFromFile(outf)
ynw = multilayer.MultiLayerGraph.YearNetwork()
ynw.update(ynw_load)
transform = {"name": "title",
"func": str,
"default": ""}
ynw = ynw.filter_es(title=test_title, transform=[transform])
titles = []
for y, gr in ynw.items():
for i in gr.vs["inv"]:
if test_title == i:
titles.append(i)
self.assertEqual(len(titles), 0, "%s should NOT be in title anymore in year %s!" % (";".join(titles), y))
with open("/tmp/ynws.tgz", "wb") as outf:
igraphx.writeGraphsToFile(ynw, outf)
def test_filter_ynw(self):
pt = "data/co-author-social_years_simplified_3.tgz"
with open(pt, "rb") as outf:
......@@ -52,6 +77,22 @@ class TestMultiLayerGraph(TestCase):
with open("/tmp/ynws.tgz", "wb") as outf:
igraphx.writeGraphsToFile(ynw,outf)
def test_filter_es(self):
ma = MultiLayerGraph.load("data/co_author.pickle")
# print(ma.multi_layer.vs["label"])
name = "institution_University"
ma2 = ma.filter_es(name=name)
insts = []
for i in ma2.multi_layer.es["title"]:
if i == name:
insts.append(i)
self.assertEqual(len(insts), 0, "%s should NOT be in the label anymore!" % ";".join(insts))
with open("/tmp/out.tgz", "wb") as outf:
ma2.writeGraphsToFile(outf)
def test_filter(self):
ma = MultiLayerGraph.load("data/co_author.pickle")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment