From 474b5dd2cf4ddf2a0e7f121825776e1668f83b3d Mon Sep 17 00:00:00 2001 From: renzocom Date: Fri, 7 Dec 2018 15:02:48 -0600 Subject: [PATCH 01/17] Adds extended purviews in find_causal_link() --- pyphi/actual.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/pyphi/actual.py b/pyphi/actual.py index 4541128b6..8f6e9583f 100644 --- a/pyphi/actual.py +++ b/pyphi/actual.py @@ -411,13 +411,29 @@ def find_causal_link(self, direction, mechanism, purviews=False, max_ria = _null_ac_ria(self.mechanism_state(direction), direction, mechanism, None) else: - # This max should be most positive - max_ria = max(self.find_mip(direction, mechanism, purview, - allow_neg) - for purview in purviews) + # Finds rias with maximum alpha + all_ria = [self.find_mip(direction, mechanism, purview,allow_neg) + for purview in purviews] + max_alpha = max(all_ria).alpha + max_rias = [ria for ria in all_ria if ria.alpha==max_alpha] - # Construct the corresponding CausalLink - return CausalLink(max_ria) + # Selected rias whose purview is not a superset of any other + purviews = [ria.purview for ria in max_rias] + extended_purview = [] + for purview in purviews: + is_not_superset = not np.any([set(purview).issuperset(set(purview2)) and + set(purview) > set(purview2) for purview2 in purviews]) + if is_not_superset: + extended_purview.append(purview) + + max_ria = max(max_rias) + + # Construct the corresponding CausalLink including all equivalent purviews + causal_link = CausalLink(max_ria) + causal_link._extended_purview = tuple(extended_purview) + + + return causal_link def find_actual_cause(self, mechanism, purviews=False): """Return the actual cause of a mechanism.""" From 015c275fdf91fdd53ac15b3063524c8f53f8dd1d Mon Sep 17 00:00:00 2001 From: renzocom Date: Fri, 7 Dec 2018 15:03:41 -0600 Subject: [PATCH 02/17] Added fmt_extended_purview() formatting --- pyphi/models/fmt.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/pyphi/models/fmt.py b/pyphi/models/fmt.py index 931f14e80..1a9457672 100644 --- a/pyphi/models/fmt.py +++ b/pyphi/models/fmt.py @@ -425,17 +425,32 @@ def fmt_repertoire(r): return box('\n'.join(lines)) +def fmt_extended_purview(extended_purview, node_labels=None): + """Format an extended purview (multiple purviews.""" + purviews = [fmt_mechanism(purview, node_labels) for purview in extended_purview] + return '[' + ', '.join(purviews) + ']' def fmt_ac_ria(ria): """Format an AcRepertoireIrreducibilityAnalysis.""" - causality = { - Direction.CAUSE: (fmt_mechanism(ria.purview, ria.node_labels), - ARROW_LEFT, - fmt_mechanism(ria.mechanism, ria.node_labels)), - Direction.EFFECT: (fmt_mechanism(ria.mechanism, ria.node_labels), - ARROW_RIGHT, - fmt_mechanism(ria.purview, ria.node_labels)) - }[ria.direction] + if hasattr(ria, '_extended_purview') and len(ria._extended_purview)>1: + causality = { + Direction.CAUSE: (fmt_extended_purview(ria._extended_purview, ria.node_labels), + ARROW_LEFT, + fmt_mechanism(ria.mechanism, ria.node_labels)), + Direction.EFFECT: (fmt_mechanism(ria.mechanism, ria.node_labels), + ARROW_RIGHT, + fmt_extended_purview(ria._extended_purview, ria.node_labels)) + }[ria.direction] + + else: + causality = { + Direction.CAUSE: (fmt_mechanism(ria.purview, ria.node_labels), + ARROW_LEFT, + fmt_mechanism(ria.mechanism, ria.node_labels)), + Direction.EFFECT: (fmt_mechanism(ria.mechanism, ria.node_labels), + ARROW_RIGHT, + fmt_mechanism(ria.purview, ria.node_labels)) + }[ria.direction] causality = ' '.join(causality) return '{ALPHA} = {alpha} {causality}'.format( From 4169d4529e39fb9864a2e8b60c06f9ffeb0add8e Mon Sep 17 00:00:00 2001 From: renzocom Date: Fri, 7 Dec 2018 15:19:20 -0600 Subject: [PATCH 03/17] just syntax... --- pyphi/actual.py | 6 +----- pyphi/models/fmt.py | 1 - 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/pyphi/actual.py b/pyphi/actual.py index 8f6e9583f..fac5d5926 100644 --- a/pyphi/actual.py +++ b/pyphi/actual.py @@ -426,13 +426,9 @@ def find_causal_link(self, direction, mechanism, purviews=False, if is_not_superset: extended_purview.append(purview) - max_ria = max(max_rias) - # Construct the corresponding CausalLink including all equivalent purviews - causal_link = CausalLink(max_ria) + causal_link = CausalLink(max(max_rias)) causal_link._extended_purview = tuple(extended_purview) - - return causal_link def find_actual_cause(self, mechanism, purviews=False): diff --git a/pyphi/models/fmt.py b/pyphi/models/fmt.py index 1a9457672..479302c91 100644 --- a/pyphi/models/fmt.py +++ b/pyphi/models/fmt.py @@ -458,7 +458,6 @@ def fmt_ac_ria(ria): alpha=round(ria.alpha, 4), causality=causality) - def fmt_account(account, title=None): """Format an Account or a DirectedAccount.""" if title is None: From 0765209454efece3a017355c164aff0159f2bc77 Mon Sep 17 00:00:00 2001 From: renzocom Date: Fri, 7 Dec 2018 15:57:38 -0600 Subject: [PATCH 04/17] add CausalLink._ria._extended_purview attribute --- pyphi/actual.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pyphi/actual.py b/pyphi/actual.py index fac5d5926..0be918a82 100644 --- a/pyphi/actual.py +++ b/pyphi/actual.py @@ -426,9 +426,10 @@ def find_causal_link(self, direction, mechanism, purviews=False, if is_not_superset: extended_purview.append(purview) - # Construct the corresponding CausalLink including all equivalent purviews + # Construct the corresponding CausalLink including all equivalent purviews in hidden attribute causal_link = CausalLink(max(max_rias)) causal_link._extended_purview = tuple(extended_purview) + causal_link._ria._extended_purview = tuple(extended_purview) return causal_link def find_actual_cause(self, mechanism, purviews=False): From 66191df4a09bc37a43f54bb05913e25d58370463 Mon Sep 17 00:00:00 2001 From: renzocom Date: Fri, 7 Dec 2018 16:43:53 -0600 Subject: [PATCH 05/17] fix return of find_causal_link() ls --- pyphi/actual.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pyphi/actual.py b/pyphi/actual.py index 0be918a82..754a8e43e 100644 --- a/pyphi/actual.py +++ b/pyphi/actual.py @@ -410,6 +410,7 @@ def find_causal_link(self, direction, mechanism, purviews=False, if not purviews: max_ria = _null_ac_ria(self.mechanism_state(direction), direction, mechanism, None) + return CausalLink(max_ria) else: # Finds rias with maximum alpha all_ria = [self.find_mip(direction, mechanism, purview,allow_neg) @@ -430,7 +431,7 @@ def find_causal_link(self, direction, mechanism, purviews=False, causal_link = CausalLink(max(max_rias)) causal_link._extended_purview = tuple(extended_purview) causal_link._ria._extended_purview = tuple(extended_purview) - return causal_link + return causal_link def find_actual_cause(self, mechanism, purviews=False): """Return the actual cause of a mechanism.""" From 756b7821a4980e0f78fa09c27e4cd4f292dd95b8 Mon Sep 17 00:00:00 2001 From: renzocom Date: Thu, 13 Dec 2018 15:32:18 -0600 Subject: [PATCH 06/17] Add attribute to class. --- pyphi/actual.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/pyphi/actual.py b/pyphi/actual.py index 754a8e43e..371063f78 100644 --- a/pyphi/actual.py +++ b/pyphi/actual.py @@ -426,12 +426,7 @@ def find_causal_link(self, direction, mechanism, purviews=False, set(purview) > set(purview2) for purview2 in purviews]) if is_not_superset: extended_purview.append(purview) - - # Construct the corresponding CausalLink including all equivalent purviews in hidden attribute - causal_link = CausalLink(max(max_rias)) - causal_link._extended_purview = tuple(extended_purview) - causal_link._ria._extended_purview = tuple(extended_purview) - return causal_link + return CausalLink(max(max_rias),tuple(extended_purview)) def find_actual_cause(self, mechanism, purviews=False): """Return the actual cause of a mechanism.""" From b6d264568a424987d45b5e171ae9989c68f0d958 Mon Sep 17 00:00:00 2001 From: renzocom Date: Thu, 13 Dec 2018 15:56:27 -0600 Subject: [PATCH 07/17] Add formatting of CausalLink and Account with extended purviews. --- pyphi/models/actual_causation.py | 12 +++++++--- pyphi/models/fmt.py | 41 ++++++++++++++------------------ 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/pyphi/models/actual_causation.py b/pyphi/models/actual_causation.py index 08416baa3..2816cf008 100644 --- a/pyphi/models/actual_causation.py +++ b/pyphi/models/actual_causation.py @@ -131,8 +131,9 @@ class CausalLink(cmp.Orderable): up to |PRECISION|, the size of the mechanism is compared. """ - def __init__(self, ria): + def __init__(self, ria, extended_purview=None): self._ria = ria + self._extended_purview = extended_purview @property def alpha(self): @@ -163,6 +164,11 @@ def purview(self): """ return self._ria.purview + @property + def extended_purview(self): + """tuple of tuple[int]: List of purviews over which this causal link is equivalentymaximally irreducible.""" + return self._extended_purview + @property def ria(self): """AcRepertoireIrreducibilityAnalysis: The irreducibility analysis for @@ -175,10 +181,10 @@ def node_labels(self): return self._ria.node_labels def __repr__(self): - return fmt.make_repr(self, ['ria']) + return fmt.make_repr(self, ['ria','extended_purview']) def __str__(self): - return "CausalLink\n" + fmt.indent(fmt.fmt_ac_ria(self.ria)) + return "CausalLink\n" + fmt.indent(fmt.fmt_causal_link(self)) unorderable_unless_eq = \ AcRepertoireIrreducibilityAnalysis.unorderable_unless_eq diff --git a/pyphi/models/fmt.py b/pyphi/models/fmt.py index 479302c91..f595ff919 100644 --- a/pyphi/models/fmt.py +++ b/pyphi/models/fmt.py @@ -426,31 +426,26 @@ def fmt_repertoire(r): return box('\n'.join(lines)) def fmt_extended_purview(extended_purview, node_labels=None): - """Format an extended purview (multiple purviews.""" + """Format an extended purview.""" purviews = [fmt_mechanism(purview, node_labels) for purview in extended_purview] return '[' + ', '.join(purviews) + ']' -def fmt_ac_ria(ria): - """Format an AcRepertoireIrreducibilityAnalysis.""" - if hasattr(ria, '_extended_purview') and len(ria._extended_purview)>1: - causality = { - Direction.CAUSE: (fmt_extended_purview(ria._extended_purview, ria.node_labels), - ARROW_LEFT, - fmt_mechanism(ria.mechanism, ria.node_labels)), - Direction.EFFECT: (fmt_mechanism(ria.mechanism, ria.node_labels), - ARROW_RIGHT, - fmt_extended_purview(ria._extended_purview, ria.node_labels)) - }[ria.direction] +def fmt_causal_link(causal_link): + """Format a CausalLink.""" + return fmt_ac_ria(causal_link, causal_link.extended_purview) - else: - causality = { - Direction.CAUSE: (fmt_mechanism(ria.purview, ria.node_labels), - ARROW_LEFT, - fmt_mechanism(ria.mechanism, ria.node_labels)), - Direction.EFFECT: (fmt_mechanism(ria.mechanism, ria.node_labels), - ARROW_RIGHT, - fmt_mechanism(ria.purview, ria.node_labels)) - }[ria.direction] +def fmt_ac_ria(ria, extended_purview=None): + """Format an AcRepertoireIrreducibilityAnalysis.""" + causality = { + Direction.CAUSE: (fmt_mechanism(ria.purview, ria.node_labels) if extended_purview is None + else fmt_extended_purview(ria._extended_purview, ria.node_labels), + ARROW_LEFT, + fmt_mechanism(ria.mechanism, ria.node_labels)), + Direction.EFFECT: (fmt_mechanism(ria.mechanism, ria.node_labels), + ARROW_RIGHT, + fmt_mechanism(ria.purview, ria.node_labels) if extended_purview is None + else fmt_extended_purview(ria._extended_purview, ria.node_labels)) + }[ria.direction] causality = ' '.join(causality) return '{ALPHA} = {alpha} {causality}'.format( @@ -468,9 +463,9 @@ def fmt_account(account, title=None): body = '' body += 'Irreducible effects\n' - body += '\n'.join(fmt_ac_ria(m) for m in account.irreducible_effects) + body += '\n'.join(fmt_causal_link(m) for m in account.irreducible_effects) body += '\nIrreducible causes\n' - body += '\n'.join(fmt_ac_ria(m) for m in account.irreducible_causes) + body += '\n'.join(fmt_causal_link(m) for m in account.irreducible_causes) return '\n' + header(title, body, under_char='*') From 99356a1eb186966d6d9274ae5e76e0c59fb512d2 Mon Sep 17 00:00:00 2001 From: renzocom Date: Thu, 13 Dec 2018 16:10:41 -0600 Subject: [PATCH 08/17] Add documentation of extended_purview attribute. --- pyphi/models/actual_causation.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pyphi/models/actual_causation.py b/pyphi/models/actual_causation.py index 2816cf008..cad0829e9 100644 --- a/pyphi/models/actual_causation.py +++ b/pyphi/models/actual_causation.py @@ -166,7 +166,10 @@ def purview(self): @property def extended_purview(self): - """tuple of tuple[int]: List of purviews over which this causal link is equivalentymaximally irreducible.""" + """tuple of tuple[int]: List of purviews over which this causal link is + (equivalently) maximally irreducible. + Note: It is reducible to the purview unless the causal link has undetermined + causes/effect.""" return self._extended_purview @property From ef1799f0902d66a568c7a6ffc82549b00262421c Mon Sep 17 00:00:00 2001 From: renzocom Date: Sat, 15 Dec 2018 18:34:21 -0600 Subject: [PATCH 09/17] Fix code style --- pyphi/actual.py | 19 +++++++++---------- pyphi/models/actual_causation.py | 8 ++++---- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/pyphi/actual.py b/pyphi/actual.py index 371063f78..6c788c066 100644 --- a/pyphi/actual.py +++ b/pyphi/actual.py @@ -413,20 +413,19 @@ def find_causal_link(self, direction, mechanism, purviews=False, return CausalLink(max_ria) else: # Finds rias with maximum alpha - all_ria = [self.find_mip(direction, mechanism, purview,allow_neg) - for purview in purviews] + all_ria = [self.find_mip(direction, mechanism, purview, allow_neg) + for purview in purviews] max_alpha = max(all_ria).alpha - max_rias = [ria for ria in all_ria if ria.alpha==max_alpha] + max_rias = [ria for ria in all_ria if ria.alpha == max_alpha] # Selected rias whose purview is not a superset of any other + def is_not_superset(purview): + return np.all([(not set(purview).issuperset(set(purview2))) or + (set(purview) == set(purview2)) for purview2 in purviews]) + purviews = [ria.purview for ria in max_rias] - extended_purview = [] - for purview in purviews: - is_not_superset = not np.any([set(purview).issuperset(set(purview2)) and - set(purview) > set(purview2) for purview2 in purviews]) - if is_not_superset: - extended_purview.append(purview) - return CausalLink(max(max_rias),tuple(extended_purview)) + extended_purview = filter(is_not_superset, purviews) + return CausalLink(max(max_rias), tuple(extended_purview)) def find_actual_cause(self, mechanism, purviews=False): """Return the actual cause of a mechanism.""" diff --git a/pyphi/models/actual_causation.py b/pyphi/models/actual_causation.py index cad0829e9..4c21caf93 100644 --- a/pyphi/models/actual_causation.py +++ b/pyphi/models/actual_causation.py @@ -166,10 +166,10 @@ def purview(self): @property def extended_purview(self): - """tuple of tuple[int]: List of purviews over which this causal link is + """tuple[tuple[int]]: List of purviews over which this causal link is (equivalently) maximally irreducible. - Note: It is reducible to the purview unless the causal link has undetermined - causes/effect.""" + Note: It is reducible to the purview unless the causal link has + undetermined causes/effect.""" return self._extended_purview @property @@ -184,7 +184,7 @@ def node_labels(self): return self._ria.node_labels def __repr__(self): - return fmt.make_repr(self, ['ria','extended_purview']) + return fmt.make_repr(self, ['ria', 'extended_purview']) def __str__(self): return "CausalLink\n" + fmt.indent(fmt.fmt_causal_link(self)) From c7aebb0e09372c3139172f5de937ec495928fb99 Mon Sep 17 00:00:00 2001 From: renzocom Date: Sat, 15 Dec 2018 18:35:38 -0600 Subject: [PATCH 10/17] Fix code style and add case in fmt_extended_purview() for single purview --- pyphi/models/fmt.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/pyphi/models/fmt.py b/pyphi/models/fmt.py index f595ff919..b911e4bb0 100644 --- a/pyphi/models/fmt.py +++ b/pyphi/models/fmt.py @@ -427,24 +427,31 @@ def fmt_repertoire(r): def fmt_extended_purview(extended_purview, node_labels=None): """Format an extended purview.""" - purviews = [fmt_mechanism(purview, node_labels) for purview in extended_purview] - return '[' + ', '.join(purviews) + ']' + if len(extended_purview)==1: + return fmt_mechanism(extended_purview[0], node_labels=node_labels) + + else: + purviews = [fmt_mechanism(purview, node_labels=node_labels) for purview in extended_purview] + return '[' + ', '.join(purviews) + ']' + def fmt_causal_link(causal_link): """Format a CausalLink.""" - return fmt_ac_ria(causal_link, causal_link.extended_purview) + return fmt_ac_ria(causal_link, extended_purview=causal_link.extended_purview) def fmt_ac_ria(ria, extended_purview=None): """Format an AcRepertoireIrreducibilityAnalysis.""" causality = { - Direction.CAUSE: (fmt_mechanism(ria.purview, ria.node_labels) if extended_purview is None - else fmt_extended_purview(ria._extended_purview, ria.node_labels), + Direction.CAUSE: (fmt_mechanism(ria.purview, ria.node_labels) + if extended_purview is None + else fmt_extended_purview(ria.extended_purview, ria.node_labels), ARROW_LEFT, fmt_mechanism(ria.mechanism, ria.node_labels)), Direction.EFFECT: (fmt_mechanism(ria.mechanism, ria.node_labels), ARROW_RIGHT, - fmt_mechanism(ria.purview, ria.node_labels) if extended_purview is None - else fmt_extended_purview(ria._extended_purview, ria.node_labels)) + fmt_mechanism(ria.purview, ria.node_labels) + if extended_purview is None + else fmt_extended_purview(ria.extended_purview, ria.node_labels)) }[ria.direction] causality = ' '.join(causality) From da38525697da8993152a0ffc084e578d5aee572e Mon Sep 17 00:00:00 2001 From: renzocom Date: Sat, 15 Dec 2018 18:43:19 -0600 Subject: [PATCH 11/17] Improve extended_purview docstring --- pyphi/models/actual_causation.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pyphi/models/actual_causation.py b/pyphi/models/actual_causation.py index 4c21caf93..d9175e56a 100644 --- a/pyphi/models/actual_causation.py +++ b/pyphi/models/actual_causation.py @@ -167,9 +167,10 @@ def purview(self): @property def extended_purview(self): """tuple[tuple[int]]: List of purviews over which this causal link is - (equivalently) maximally irreducible. - Note: It is reducible to the purview unless the causal link has - undetermined causes/effect.""" + maximally irreducible. + Note: It will contain multiple purviews iff causal link has + undetermined actual causes/effects. + """ return self._extended_purview @property From 3245dd6553c6c27a1302b0cef1bc55c9f16f3e9f Mon Sep 17 00:00:00 2001 From: renzocom Date: Sat, 15 Dec 2018 18:46:16 -0600 Subject: [PATCH 12/17] Fix code style --- pyphi/models/fmt.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pyphi/models/fmt.py b/pyphi/models/fmt.py index b911e4bb0..8c47652a4 100644 --- a/pyphi/models/fmt.py +++ b/pyphi/models/fmt.py @@ -427,12 +427,11 @@ def fmt_repertoire(r): def fmt_extended_purview(extended_purview, node_labels=None): """Format an extended purview.""" - if len(extended_purview)==1: + if len(extended_purview) == 1: return fmt_mechanism(extended_purview[0], node_labels=node_labels) - else: - purviews = [fmt_mechanism(purview, node_labels=node_labels) for purview in extended_purview] - return '[' + ', '.join(purviews) + ']' + purviews = [fmt_mechanism(purview, node_labels=node_labels) for purview in extended_purview] + return '[' + ', '.join(purviews) + ']' def fmt_causal_link(causal_link): From 15a5a8c57f058ac230d9b1feca8da0bad19a0946 Mon Sep 17 00:00:00 2001 From: Renzo Comolatti Date: Fri, 13 Sep 2019 10:53:49 +0200 Subject: [PATCH 13/17] Simplify is_not_superset() --- pyphi/actual.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pyphi/actual.py b/pyphi/actual.py index 6c788c066..1ef974322 100644 --- a/pyphi/actual.py +++ b/pyphi/actual.py @@ -418,12 +418,12 @@ def find_causal_link(self, direction, mechanism, purviews=False, max_alpha = max(all_ria).alpha max_rias = [ria for ria in all_ria if ria.alpha == max_alpha] + purviews = [ria.purview for ria in max_rias] # Selected rias whose purview is not a superset of any other def is_not_superset(purview): - return np.all([(not set(purview).issuperset(set(purview2))) or - (set(purview) == set(purview2)) for purview2 in purviews]) - - purviews = [ria.purview for ria in max_rias] + return any((set(purview).issuperset(set(other_purview))) and + (set(purview) != set(other_purview)) for other_purview in purviews) + extended_purview = filter(is_not_superset, purviews) return CausalLink(max(max_rias), tuple(extended_purview)) From 7d4a3efeeb5876ae5fda01d3b99bd81ab10064cc Mon Sep 17 00:00:00 2001 From: Renzo Comolatti Date: Fri, 13 Sep 2019 11:00:09 +0200 Subject: [PATCH 14/17] Simplify is_not_superset() --- pyphi/actual.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyphi/actual.py b/pyphi/actual.py index 1ef974322..ac13f27ae 100644 --- a/pyphi/actual.py +++ b/pyphi/actual.py @@ -421,9 +421,9 @@ def find_causal_link(self, direction, mechanism, purviews=False, purviews = [ria.purview for ria in max_rias] # Selected rias whose purview is not a superset of any other def is_not_superset(purview): - return any((set(purview).issuperset(set(other_purview))) and - (set(purview) != set(other_purview)) for other_purview in purviews) - + return all((not set(purview).issuperset(set(other_purview))) or + (set(purview) == set(other_purview)) for other_purview in purviews) + extended_purview = filter(is_not_superset, purviews) return CausalLink(max(max_rias), tuple(extended_purview)) From 8cd05bf3ebea090d61659e1e41129b68c899ac91 Mon Sep 17 00:00:00 2001 From: Renzo Comolatti Date: Fri, 13 Sep 2019 11:22:47 +0200 Subject: [PATCH 15/17] Simplify find_causal_link() and cast extended_purview to tuple --- pyphi/actual.py | 8 +++----- pyphi/models/actual_causation.py | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/pyphi/actual.py b/pyphi/actual.py index ac13f27ae..b420994f4 100644 --- a/pyphi/actual.py +++ b/pyphi/actual.py @@ -415,17 +415,15 @@ def find_causal_link(self, direction, mechanism, purviews=False, # Finds rias with maximum alpha all_ria = [self.find_mip(direction, mechanism, purview, allow_neg) for purview in purviews] - max_alpha = max(all_ria).alpha - max_rias = [ria for ria in all_ria if ria.alpha == max_alpha] - - purviews = [ria.purview for ria in max_rias] + max_ria = max(all_rias) + purviews = [ria.purview for ria in all_ria if ria.alpha == max_ria.alpha] # Selected rias whose purview is not a superset of any other def is_not_superset(purview): return all((not set(purview).issuperset(set(other_purview))) or (set(purview) == set(other_purview)) for other_purview in purviews) extended_purview = filter(is_not_superset, purviews) - return CausalLink(max(max_rias), tuple(extended_purview)) + return CausalLink(max_ria, tuple(extended_purview)) def find_actual_cause(self, mechanism, purviews=False): """Return the actual cause of a mechanism.""" diff --git a/pyphi/models/actual_causation.py b/pyphi/models/actual_causation.py index d9175e56a..01dfac99f 100644 --- a/pyphi/models/actual_causation.py +++ b/pyphi/models/actual_causation.py @@ -133,7 +133,7 @@ class CausalLink(cmp.Orderable): def __init__(self, ria, extended_purview=None): self._ria = ria - self._extended_purview = extended_purview + self._extended_purview = tuple(extended_purview) @property def alpha(self): From 30d6a2aea1f07080b5d9d2b574e3e61fbb7c049b Mon Sep 17 00:00:00 2001 From: Renzo Comolatti Date: Fri, 13 Sep 2019 11:23:44 +0200 Subject: [PATCH 16/17] Simplify find_causal_link() and cast extended_purview to tuple --- pyphi/actual.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/pyphi/actual.py b/pyphi/actual.py index b420994f4..c04833fae 100644 --- a/pyphi/actual.py +++ b/pyphi/actual.py @@ -411,19 +411,19 @@ def find_causal_link(self, direction, mechanism, purviews=False, max_ria = _null_ac_ria(self.mechanism_state(direction), direction, mechanism, None) return CausalLink(max_ria) - else: - # Finds rias with maximum alpha - all_ria = [self.find_mip(direction, mechanism, purview, allow_neg) - for purview in purviews] - max_ria = max(all_rias) - purviews = [ria.purview for ria in all_ria if ria.alpha == max_ria.alpha] - # Selected rias whose purview is not a superset of any other - def is_not_superset(purview): - return all((not set(purview).issuperset(set(other_purview))) or - (set(purview) == set(other_purview)) for other_purview in purviews) - - extended_purview = filter(is_not_superset, purviews) - return CausalLink(max_ria, tuple(extended_purview)) + + # Finds rias with maximum alpha + all_ria = [self.find_mip(direction, mechanism, purview, allow_neg) + for purview in purviews] + max_ria = max(all_rias) + purviews = [ria.purview for ria in all_ria if ria.alpha == max_ria.alpha] + # Selected rias whose purview is not a superset of any other + def is_not_superset(purview): + return all((not set(purview).issuperset(set(other_purview))) or + (set(purview) == set(other_purview)) for other_purview in purviews) + + extended_purview = filter(is_not_superset, purviews) + return CausalLink(max_ria, tuple(extended_purview)) def find_actual_cause(self, mechanism, purviews=False): """Return the actual cause of a mechanism.""" From 40a0c631ca5bf35c21dea086b4e719f6869fc46e Mon Sep 17 00:00:00 2001 From: Renzo Comolatti Date: Fri, 13 Sep 2019 12:50:22 +0200 Subject: [PATCH 17/17] Minor style and syntax improvements --- pyphi/actual.py | 4 ++-- pyphi/models/actual_causation.py | 6 ++++-- pyphi/utils.py | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/pyphi/actual.py b/pyphi/actual.py index c04833fae..b9e4e1d47 100644 --- a/pyphi/actual.py +++ b/pyphi/actual.py @@ -413,9 +413,9 @@ def find_causal_link(self, direction, mechanism, purviews=False, return CausalLink(max_ria) # Finds rias with maximum alpha - all_ria = [self.find_mip(direction, mechanism, purview, allow_neg) + all_ria = [self.find_mip(direction, mechanism, purview, allow_neg=allow_neg) for purview in purviews] - max_ria = max(all_rias) + max_ria = max(all_ria) purviews = [ria.purview for ria in all_ria if ria.alpha == max_ria.alpha] # Selected rias whose purview is not a superset of any other def is_not_superset(purview): diff --git a/pyphi/models/actual_causation.py b/pyphi/models/actual_causation.py index 01dfac99f..501a3ff98 100644 --- a/pyphi/models/actual_causation.py +++ b/pyphi/models/actual_causation.py @@ -133,7 +133,7 @@ class CausalLink(cmp.Orderable): def __init__(self, ria, extended_purview=None): self._ria = ria - self._extended_purview = tuple(extended_purview) + self._extended_purview = tuple(extended_purview) if extended_purview is not None else None @property def alpha(self): @@ -168,8 +168,10 @@ def purview(self): def extended_purview(self): """tuple[tuple[int]]: List of purviews over which this causal link is maximally irreducible. + Note: It will contain multiple purviews iff causal link has - undetermined actual causes/effects. + undetermined actual causes/effects (e.g. two irreducible causes with same alpha + over different purviews). """ return self._extended_purview diff --git a/pyphi/utils.py b/pyphi/utils.py index c2a9948b7..c6bffbabc 100644 --- a/pyphi/utils.py +++ b/pyphi/utils.py @@ -198,7 +198,7 @@ def load_data(directory, num): def get_path(i): # pylint: disable=missing-docstring return os.path.join(root, 'data', directory, str(i) + '.npy') - return [np.load(get_path(i)) for i in range(num)] + return [np.load(get_path(i), allow_pickle=True) for i in range(num)] # Using ``decorator`` preserves the function signature of the wrapped function,