Skip to content

Commit

Permalink
Fix bug in manual adding of conditioning variables
Browse files Browse the repository at this point in the history
Fix bug in manual adding of conditioning variables. Convert
variables to lags bevor removing them from the candidate set.
Adjust unit tests accordingly.
  • Loading branch information
pwollstadt committed Jan 14, 2019
1 parent fa18555 commit 33eda1e
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 56 deletions.
3 changes: 2 additions & 1 deletion idtxl/network_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ def _remove_forced_conditionals(self, candidate_set):
cond = self.settings['add_conditionals']
if type(cond) is tuple: # easily add single variable
cond = [cond]
candidate_set = list(set(candidate_set).difference(set(cond)))
cond_idx = self._lag_to_idx(cond)
candidate_set = list(set(candidate_set).difference(set(cond_idx)))
return candidate_set

def _append_selected_vars_idx(self, idx):
Expand Down
23 changes: 12 additions & 11 deletions test/test_active_information_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,26 +255,27 @@ def test_define_candidates():
# Test if candidates that are added manually to the conditioning set are
# removed from the candidate set.
nw = ActiveInformationStorage()
nw.current_value = current_val
settings = [
{'add_conditionals': None},
{'add_conditionals': (2, 3)},
{'add_conditionals': [(2, 3), (4, 1)]}]
{'add_conditionals': [(2, 3), (4, 1)]},
{'add_conditionals': [(1, 9)]},
{'add_conditionals': [(1, 9), (2, 3), (4, 1)]}]
for s in settings:
nw.settings = s
candidates = nw._define_candidates(procs, samples)
assert (1, 9) in candidates, 'Sample missing from candidates: (1, 9).'
assert (1, 6) in candidates, 'Sample missing from candidates: (1, 6).'
assert (1, 3) in candidates, 'Sample missing from candidates: (1, 3).'

settings = [
{'add_conditionals': [(1, 9)]},
{'add_conditionals': [(1, 9), (2, 3), (4, 1)]}]
for s in settings:
nw.settings = s
candidates = nw._define_candidates(procs, samples)
assert (1, 9) not in candidates, 'Sample missing from candidates: (1, 9).'
assert (1, 6) in candidates, 'Sample missing from candidates: (1, 6).'
assert (1, 3) in candidates, 'Sample missing from candidates: (1, 3).'
if s['add_conditionals'] is not None:
if type(s['add_conditionals']) is tuple:
cond_ind = nw._lag_to_idx([s['add_conditionals']])
else:
cond_ind = nw._lag_to_idx(s['add_conditionals'])
for c in cond_ind:
assert c not in candidates, (
'Sample added erronously to candidates: {}.'.format(c))


if __name__ == '__main__':
Expand Down
23 changes: 12 additions & 11 deletions test/test_bivariate_mi.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,26 +372,27 @@ def test_define_candidates():
# Test if candidates that are added manually to the conditioning set are
# removed from the candidate set.
nw = BivariateMI()
nw.current_value = current_val
settings = [
{'add_conditionals': None},
{'add_conditionals': (2, 3)},
{'add_conditionals': [(2, 3), (4, 1)]}]
{'add_conditionals': [(2, 3), (4, 1)]},
{'add_conditionals': [(1, 9)]},
{'add_conditionals': [(1, 9), (2, 3), (4, 1)]}]
for s in settings:
nw.settings = s
candidates = nw._define_candidates(procs, samples)
assert (1, 9) in candidates, 'Sample missing from candidates: (1, 9).'
assert (1, 6) in candidates, 'Sample missing from candidates: (1, 6).'
assert (1, 3) in candidates, 'Sample missing from candidates: (1, 3).'

settings = [
{'add_conditionals': [(1, 9)]},
{'add_conditionals': [(1, 9), (2, 3), (4, 1)]}]
for s in settings:
nw.settings = s
candidates = nw._define_candidates(procs, samples)
assert (1, 9) not in candidates, 'Sample missing from candidates: (1, 9).'
assert (1, 6) in candidates, 'Sample missing from candidates: (1, 6).'
assert (1, 3) in candidates, 'Sample missing from candidates: (1, 3).'
if s['add_conditionals'] is not None:
if type(s['add_conditionals']) is tuple:
cond_ind = nw._lag_to_idx([s['add_conditionals']])
else:
cond_ind = nw._lag_to_idx(s['add_conditionals'])
for c in cond_ind:
assert c not in candidates, (
'Sample added erronously to candidates: {}.'.format(c))

@jpype_missing
def test_analyse_network():
Expand Down
23 changes: 12 additions & 11 deletions test/test_bivariate_te.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,26 +372,27 @@ def test_define_candidates():
# Test if candidates that are added manually to the conditioning set are
# removed from the candidate set.
nw = BivariateTE()
nw.current_value = current_val
settings = [
{'add_conditionals': None},
{'add_conditionals': (2, 3)},
{'add_conditionals': [(2, 3), (4, 1)]}]
{'add_conditionals': [(2, 3), (4, 1)]},
{'add_conditionals': [(1, 9)]},
{'add_conditionals': [(1, 9), (2, 3), (4, 1)]}]
for s in settings:
nw.settings = s
candidates = nw._define_candidates(procs, samples)
assert (1, 9) in candidates, 'Sample missing from candidates: (1, 9).'
assert (1, 6) in candidates, 'Sample missing from candidates: (1, 6).'
assert (1, 3) in candidates, 'Sample missing from candidates: (1, 3).'

settings = [
{'add_conditionals': [(1, 9)]},
{'add_conditionals': [(1, 9), (2, 3), (4, 1)]}]
for s in settings:
nw.settings = s
candidates = nw._define_candidates(procs, samples)
assert (1, 9) not in candidates, 'Sample missing from candidates: (1, 9).'
assert (1, 6) in candidates, 'Sample missing from candidates: (1, 6).'
assert (1, 3) in candidates, 'Sample missing from candidates: (1, 3).'
if s['add_conditionals'] is not None:
if type(s['add_conditionals']) is tuple:
cond_ind = nw._lag_to_idx([s['add_conditionals']])
else:
cond_ind = nw._lag_to_idx(s['add_conditionals'])
for c in cond_ind:
assert c not in candidates, (
'Sample added erronously to candidates: {}.'.format(c))


@jpype_missing
Expand Down
23 changes: 12 additions & 11 deletions test/test_multivariate_mi.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,26 +363,27 @@ def test_define_candidates():
# Test if candidates that are added manually to the conditioning set are
# removed from the candidate set.
nw = MultivariateMI()
nw.current_value = current_val
settings = [
{'add_conditionals': None},
{'add_conditionals': (2, 3)},
{'add_conditionals': [(2, 3), (4, 1)]}]
{'add_conditionals': [(2, 3), (4, 1)]},
{'add_conditionals': [(1, 9)]},
{'add_conditionals': [(1, 9), (2, 3), (4, 1)]}]
for s in settings:
nw.settings = s
candidates = nw._define_candidates(procs, samples)
assert (1, 9) in candidates, 'Sample missing from candidates: (1, 9).'
assert (1, 6) in candidates, 'Sample missing from candidates: (1, 6).'
assert (1, 3) in candidates, 'Sample missing from candidates: (1, 3).'

settings = [
{'add_conditionals': [(1, 9)]},
{'add_conditionals': [(1, 9), (2, 3), (4, 1)]}]
for s in settings:
nw.settings = s
candidates = nw._define_candidates(procs, samples)
assert (1, 9) not in candidates, 'Sample missing from candidates: (1, 9).'
assert (1, 6) in candidates, 'Sample missing from candidates: (1, 6).'
assert (1, 3) in candidates, 'Sample missing from candidates: (1, 3).'
if s['add_conditionals'] is not None:
if type(s['add_conditionals']) is tuple:
cond_ind = nw._lag_to_idx([s['add_conditionals']])
else:
cond_ind = nw._lag_to_idx(s['add_conditionals'])
for c in cond_ind:
assert c not in candidates, (
'Sample added erronously to candidates: {}.'.format(c))


@jpype_missing
Expand Down
23 changes: 12 additions & 11 deletions test/test_multivariate_te.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,26 +368,27 @@ def test_define_candidates():
# Test if candidates that are added manually to the conditioning set are
# removed from the candidate set.
nw = MultivariateTE()
nw.current_value = current_val
settings = [
{'add_conditionals': None},
{'add_conditionals': (2, 3)},
{'add_conditionals': [(2, 3), (4, 1)]}]
{'add_conditionals': [(2, 3), (4, 1)]},
{'add_conditionals': [(1, 9)]},
{'add_conditionals': [(1, 9), (2, 3), (4, 1)]}]
for s in settings:
nw.settings = s
candidates = nw._define_candidates(procs, samples)
assert (1, 9) in candidates, 'Sample missing from candidates: (1, 9).'
assert (1, 6) in candidates, 'Sample missing from candidates: (1, 6).'
assert (1, 3) in candidates, 'Sample missing from candidates: (1, 3).'

settings = [
{'add_conditionals': [(1, 9)]},
{'add_conditionals': [(1, 9), (2, 3), (4, 1)]}]
for s in settings:
nw.settings = s
candidates = nw._define_candidates(procs, samples)
assert (1, 9) not in candidates, 'Sample missing from candidates: (1, 9).'
assert (1, 6) in candidates, 'Sample missing from candidates: (1, 6).'
assert (1, 3) in candidates, 'Sample missing from candidates: (1, 3).'
if s['add_conditionals'] is not None:
if type(s['add_conditionals']) is tuple:
cond_ind = nw._lag_to_idx([s['add_conditionals']])
else:
cond_ind = nw._lag_to_idx(s['add_conditionals'])
for c in cond_ind:
assert c not in candidates, (
'Sample added erronously to candidates: {}.'.format(c))


@jpype_missing
Expand Down

0 comments on commit 33eda1e

Please sign in to comment.