Skip to content

Commit e4c9f40

Browse files
author
CindeeM
committed
NF+TST: added tools to convert different formats of partition.index, partition.community for refactor of code
1 parent 4557b2c commit e4c9f40

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

brainx/tests/test_util.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,46 @@
1717
# Functions
1818
#-----------------------------------------------------------------------------
1919

20+
def make_testlists():
21+
listlist = [[0,1,2],[3,4,5],[6,7,8,9]]
22+
dictset = {val:set(item) for val, item in enumerate(listlist)}
23+
listset = [set(item) for item in listlist]
24+
return listlist, listset, dictset
25+
26+
def test_dictset_to_listset():
27+
_, listset, dictset = make_testlists()
28+
new_list_set = util.dictset_to_listset(dictset)
29+
npt.assert_equal(new_list_set, listset)
30+
with npt.assert_raises(ValueError):
31+
tmp = util.dictset_to_listset(listset)
32+
33+
def test_listset_to_dictset():
34+
_, listset, dictset = make_testlists()
35+
new_dict_set = util.listset_to_dictset(listset)
36+
npt.assert_equal(new_dict_set, dictset)
37+
# capture wrong input type
38+
with npt.assert_raises(ValueError):
39+
tmp = util.listset_to_dictset(dictset)
40+
41+
def test_no_repeats_in_listlist():
42+
jnk = [[0,1,2],[3,4,5]] # all unique
43+
nt.assert_true(util._no_repeats_in_listlist(jnk))
44+
jnk = [[0,1,2], [0,1,2]]
45+
nt.assert_false(util._no_repeats_in_listlist(jnk))
46+
with npt.assert_raises(ValueError):
47+
util._no_repeats_in_listlist({0:0})
48+
with npt.assert_raises(ValueError):
49+
util._no_repeats_in_listlist([set([0,1,2])])
50+
51+
def test_listlist_to_listset():
52+
listlist, listset, _ = make_testlists()
53+
new_listset = util.listlist_to_listset(listlist)
54+
npt.assert_equal(new_listset, listset)
55+
with npt.assert_raises(ValueError):
56+
util.listlist_to_listset([[0,1,2],[0,1,2]])
57+
with npt.assert_raises(ValueError):
58+
util.listlist_to_listset({})
59+
2060
def test_slice_data():
2161
subcond, blocks, subjects, nodes = 5, 10, 20, 4
2262
data_4d = np.ones((blocks, subjects, nodes, nodes))

brainx/util.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,43 @@
1212
#-----------------------------------------------------------------------------
1313
# Functions
1414
#-----------------------------------------------------------------------------
15+
16+
def dictset_to_listset(dict_set):
17+
""" converts a dict of sets to a list of sets
18+
for converting partition.community objects"""
19+
if (type(dict_set) == type({}) \
20+
and all(type(x) == type(set()) for x in dict_set.values())):
21+
return dict_set.values()
22+
23+
raise ValueError('{0} is not a dict of sets'.format(dict_set))
24+
25+
def listset_to_dictset(list_set):
26+
""" converts a list of sets to a dict of sets
27+
for converting partition.community objects"""
28+
## check input is dict of sets
29+
if (type(list_set)==type([]) and \
30+
all(type(x) == type(set()) for x in list_set)):
31+
return {val: value for val, value in enumerate(list_set)}
32+
raise ValueError('{0} is not a list of sets'.format(list_set))
33+
34+
def _no_repeats_in_listlist(list_list):
35+
""" checks for duplicates in list of lists
36+
returns True or False"""
37+
if type(list_list) == type([]) and \
38+
all(type(x) == type([]) for x in list_list):
39+
allitems = [item for sublist in list_list for item in sublist]
40+
return sorted(allitems) == [x for x in sorted(set(allitems))]
41+
raise ValueError('{0} is not a list of lists'.format(list_list))
42+
43+
def listlist_to_listset(list_list):
44+
""" converts list of lists to a list of sets (with check)
45+
for converting partition.community objects"""
46+
if _no_repeats_in_listlist(list_list):
47+
return [set(x) for x in list_list]
48+
else:
49+
raise ValueError('found duplicate(s) in {0}, cannot validly format to '\
50+
'list of sets'.format(list_list))
51+
1552
def slice_data(data, sub, block, subcond=None):
1653
""" pull symmetric matrix from data block (4D or 5D)
1754

0 commit comments

Comments
 (0)