idx int64 0 251k | question stringlengths 53 3.53k | target stringlengths 5 1.23k | len_question int64 20 893 | len_target int64 3 238 |
|---|---|---|---|---|
247,800 | def find_mip ( self , direction , mechanism , purview ) : if not purview : return _null_ria ( direction , mechanism , purview ) # Calculate the unpartitioned repertoire to compare against the # partitioned ones. repertoire = self . repertoire ( direction , mechanism , purview ) def _mip ( phi , partition , partitioned_repertoire ) : # Prototype of MIP with already known data # TODO: Use properties here to infer mechanism and purview from # partition yet access them with `.mechanism` and `.purview`. return RepertoireIrreducibilityAnalysis ( phi = phi , direction = direction , mechanism = mechanism , purview = purview , partition = partition , repertoire = repertoire , partitioned_repertoire = partitioned_repertoire , node_labels = self . node_labels ) # State is unreachable - return 0 instead of giving nonsense results if ( direction == Direction . CAUSE and np . all ( repertoire == 0 ) ) : return _mip ( 0 , None , None ) mip = _null_ria ( direction , mechanism , purview , phi = float ( 'inf' ) ) for partition in mip_partitions ( mechanism , purview , self . node_labels ) : # Find the distance between the unpartitioned and partitioned # repertoire. phi , partitioned_repertoire = self . evaluate_partition ( direction , mechanism , purview , partition , repertoire = repertoire ) # Return immediately if mechanism is reducible. if phi == 0 : return _mip ( 0.0 , partition , partitioned_repertoire ) # Update MIP if it's more minimal. if phi < mip . phi : mip = _mip ( phi , partition , partitioned_repertoire ) return mip | Return the minimum information partition for a mechanism over a purview . | 406 | 13 |
247,801 | def cause_mip ( self , mechanism , purview ) : return self . find_mip ( Direction . CAUSE , mechanism , purview ) | Return the irreducibility analysis for the cause MIP . | 32 | 13 |
247,802 | def effect_mip ( self , mechanism , purview ) : return self . find_mip ( Direction . EFFECT , mechanism , purview ) | Return the irreducibility analysis for the effect MIP . | 32 | 13 |
247,803 | def phi_cause_mip ( self , mechanism , purview ) : mip = self . cause_mip ( mechanism , purview ) return mip . phi if mip else 0 | Return the |small_phi| of the cause MIP . | 43 | 13 |
247,804 | def phi_effect_mip ( self , mechanism , purview ) : mip = self . effect_mip ( mechanism , purview ) return mip . phi if mip else 0 | Return the |small_phi| of the effect MIP . | 43 | 13 |
247,805 | def phi ( self , mechanism , purview ) : return min ( self . phi_cause_mip ( mechanism , purview ) , self . phi_effect_mip ( mechanism , purview ) ) | Return the |small_phi| of a mechanism over a purview . | 47 | 15 |
247,806 | def find_mice ( self , direction , mechanism , purviews = False ) : purviews = self . potential_purviews ( direction , mechanism , purviews ) if not purviews : max_mip = _null_ria ( direction , mechanism , ( ) ) else : max_mip = max ( self . find_mip ( direction , mechanism , purview ) for purview in purviews ) if direction == Direction . CAUSE : return MaximallyIrreducibleCause ( max_mip ) elif direction == Direction . EFFECT : return MaximallyIrreducibleEffect ( max_mip ) return validate . direction ( direction ) | Return the |MIC| or |MIE| for a mechanism . | 139 | 14 |
247,807 | def phi_max ( self , mechanism ) : return min ( self . mic ( mechanism ) . phi , self . mie ( mechanism ) . phi ) | Return the |small_phi_max| of a mechanism . | 35 | 13 |
247,808 | def null_concept ( self ) : # Unconstrained cause repertoire. cause_repertoire = self . cause_repertoire ( ( ) , ( ) ) # Unconstrained effect repertoire. effect_repertoire = self . effect_repertoire ( ( ) , ( ) ) # Null cause. cause = MaximallyIrreducibleCause ( _null_ria ( Direction . CAUSE , ( ) , ( ) , cause_repertoire ) ) # Null effect. effect = MaximallyIrreducibleEffect ( _null_ria ( Direction . EFFECT , ( ) , ( ) , effect_repertoire ) ) # All together now... return Concept ( mechanism = ( ) , cause = cause , effect = effect , subsystem = self ) | Return the null concept of this subsystem . | 169 | 8 |
247,809 | def concept ( self , mechanism , purviews = False , cause_purviews = False , effect_purviews = False ) : log . debug ( 'Computing concept %s...' , mechanism ) # If the mechanism is empty, there is no concept. if not mechanism : log . debug ( 'Empty concept; returning null concept' ) return self . null_concept # Calculate the maximally irreducible cause repertoire. cause = self . mic ( mechanism , purviews = ( cause_purviews or purviews ) ) # Calculate the maximally irreducible effect repertoire. effect = self . mie ( mechanism , purviews = ( effect_purviews or purviews ) ) log . debug ( 'Found concept %s' , mechanism ) # NOTE: Make sure to expand the repertoires to the size of the # subsystem when calculating concept distance. For now, they must # remain un-expanded so the concept doesn't depend on the subsystem. return Concept ( mechanism = mechanism , cause = cause , effect = effect , subsystem = self ) | Return the concept specified by a mechanism within this subsytem . | 221 | 13 |
247,810 | def _null_ac_sia ( transition , direction , alpha = 0.0 ) : return AcSystemIrreducibilityAnalysis ( transition = transition , direction = direction , alpha = alpha , account = ( ) , partitioned_account = ( ) ) | Return an |AcSystemIrreducibilityAnalysis| with zero |big_alpha| and empty accounts . | 54 | 22 |
247,811 | def mechanism ( self ) : assert self . actual_cause . mechanism == self . actual_effect . mechanism return self . actual_cause . mechanism | The mechanism of the event . | 30 | 6 |
247,812 | def irreducible_causes ( self ) : return tuple ( link for link in self if link . direction is Direction . CAUSE ) | The set of irreducible causes in this |Account| . | 30 | 14 |
247,813 | def irreducible_effects ( self ) : return tuple ( link for link in self if link . direction is Direction . EFFECT ) | The set of irreducible effects in this |Account| . | 29 | 14 |
247,814 | def make_repr ( self , attrs ) : # TODO: change this to a closure so we can do # __repr__ = make_repr(attrs) ??? if config . REPR_VERBOSITY in [ MEDIUM , HIGH ] : return self . __str__ ( ) elif config . REPR_VERBOSITY is LOW : return '{}({})' . format ( self . __class__ . __name__ , ', ' . join ( attr + '=' + repr ( getattr ( self , attr ) ) for attr in attrs ) ) raise ValueError ( 'Invalid value for `config.REPR_VERBOSITY`' ) | Construct a repr string . | 151 | 5 |
247,815 | def indent ( lines , amount = 2 , char = ' ' ) : lines = str ( lines ) padding = amount * char return padding + ( '\n' + padding ) . join ( lines . split ( '\n' ) ) | r Indent a string . | 50 | 6 |
247,816 | def margin ( text ) : lines = str ( text ) . split ( '\n' ) return '\n' . join ( ' {} ' . format ( l ) for l in lines ) | r Add a margin to both ends of each line in the string . | 41 | 14 |
247,817 | def box ( text ) : lines = text . split ( '\n' ) width = max ( len ( l ) for l in lines ) top_bar = ( TOP_LEFT_CORNER + HORIZONTAL_BAR * ( 2 + width ) + TOP_RIGHT_CORNER ) bottom_bar = ( BOTTOM_LEFT_CORNER + HORIZONTAL_BAR * ( 2 + width ) + BOTTOM_RIGHT_CORNER ) lines = [ LINES_FORMAT_STR . format ( line = line , width = width ) for line in lines ] return top_bar + '\n' + '\n' . join ( lines ) + '\n' + bottom_bar | r Wrap a chunk of text in a box . | 158 | 10 |
247,818 | def side_by_side ( left , right ) : left_lines = list ( left . split ( '\n' ) ) right_lines = list ( right . split ( '\n' ) ) # Pad the shorter column with whitespace diff = abs ( len ( left_lines ) - len ( right_lines ) ) if len ( left_lines ) > len ( right_lines ) : fill = ' ' * len ( right_lines [ 0 ] ) right_lines += [ fill ] * diff elif len ( right_lines ) > len ( left_lines ) : fill = ' ' * len ( left_lines [ 0 ] ) left_lines += [ fill ] * diff return '\n' . join ( a + b for a , b in zip ( left_lines , right_lines ) ) + '\n' | r Put two boxes next to each other . | 179 | 9 |
247,819 | def header ( head , text , over_char = None , under_char = None , center = True ) : lines = list ( text . split ( '\n' ) ) width = max ( len ( l ) for l in lines ) # Center or left-justify if center : head = head . center ( width ) + '\n' else : head = head . ljust ( width ) + '\n' # Underline head if under_char : head = head + under_char * width + '\n' # 'Overline' head if over_char : head = over_char * width + '\n' + head return head + text | Center a head over a block of text . | 142 | 9 |
247,820 | def labels ( indices , node_labels = None ) : if node_labels is None : return tuple ( map ( str , indices ) ) return node_labels . indices2labels ( indices ) | Get the labels for a tuple of mechanism indices . | 44 | 10 |
247,821 | def fmt_number ( p ) : formatted = '{:n}' . format ( p ) if not config . PRINT_FRACTIONS : return formatted fraction = Fraction ( p ) nice = fraction . limit_denominator ( 128 ) return ( str ( nice ) if ( abs ( fraction - nice ) < constants . EPSILON and nice . denominator in NICE_DENOMINATORS ) else formatted ) | Format a number . | 91 | 4 |
247,822 | def fmt_part ( part , node_labels = None ) : def nodes ( x ) : # pylint: disable=missing-docstring return ',' . join ( labels ( x , node_labels ) ) if x else EMPTY_SET numer = nodes ( part . mechanism ) denom = nodes ( part . purview ) width = max ( 3 , len ( numer ) , len ( denom ) ) divider = HORIZONTAL_BAR * width return ( '{numer:^{width}}\n' '{divider}\n' '{denom:^{width}}' ) . format ( numer = numer , divider = divider , denom = denom , width = width ) | Format a |Part| . | 155 | 6 |
247,823 | def fmt_partition ( partition ) : if not partition : return '' parts = [ fmt_part ( part , partition . node_labels ) . split ( '\n' ) for part in partition ] times = ( ' ' , ' {} ' . format ( MULTIPLY ) , ' ' ) breaks = ( '\n' , '\n' , '' ) # No newline at the end of string between = [ times ] * ( len ( parts ) - 1 ) + [ breaks ] # Alternate [part, break, part, ..., end] elements = chain . from_iterable ( zip ( parts , between ) ) # Transform vertical stacks into horizontal lines return '' . join ( chain . from_iterable ( zip ( * elements ) ) ) | Format a |Bipartition| . | 162 | 9 |
247,824 | def fmt_ces ( c , title = None ) : if not c : return '()\n' if title is None : title = 'Cause-effect structure' concepts = '\n' . join ( margin ( x ) for x in c ) + '\n' title = '{} ({} concept{})' . format ( title , len ( c ) , '' if len ( c ) == 1 else 's' ) return header ( title , concepts , HEADER_BAR_1 , HEADER_BAR_1 ) | Format a |CauseEffectStructure| . | 115 | 9 |
247,825 | def fmt_concept ( concept ) : def fmt_cause_or_effect ( x ) : # pylint: disable=missing-docstring return box ( indent ( fmt_ria ( x . ria , verbose = False , mip = True ) , amount = 1 ) ) cause = header ( 'MIC' , fmt_cause_or_effect ( concept . cause ) ) effect = header ( 'MIE' , fmt_cause_or_effect ( concept . effect ) ) ce = side_by_side ( cause , effect ) mechanism = fmt_mechanism ( concept . mechanism , concept . node_labels ) title = 'Concept: Mechanism = {}, {} = {}' . format ( mechanism , SMALL_PHI , fmt_number ( concept . phi ) ) # Only center headers for high-verbosity output center = config . REPR_VERBOSITY is HIGH return header ( title , ce , HEADER_BAR_2 , HEADER_BAR_2 , center = center ) | Format a |Concept| . | 221 | 7 |
247,826 | def fmt_ria ( ria , verbose = True , mip = False ) : if verbose : mechanism = 'Mechanism: {}\n' . format ( fmt_mechanism ( ria . mechanism , ria . node_labels ) ) direction = '\nDirection: {}' . format ( ria . direction ) else : mechanism = '' direction = '' if config . REPR_VERBOSITY is HIGH : partition = '\n{}:\n{}' . format ( ( 'MIP' if mip else 'Partition' ) , indent ( fmt_partition ( ria . partition ) ) ) repertoire = '\nRepertoire:\n{}' . format ( indent ( fmt_repertoire ( ria . repertoire ) ) ) partitioned_repertoire = '\nPartitioned repertoire:\n{}' . format ( indent ( fmt_repertoire ( ria . partitioned_repertoire ) ) ) else : partition = '' repertoire = '' partitioned_repertoire = '' # TODO? print the two repertoires side-by-side return ( '{SMALL_PHI} = {phi}\n' '{mechanism}' 'Purview = {purview}' '{direction}' '{partition}' '{repertoire}' '{partitioned_repertoire}' ) . format ( SMALL_PHI = SMALL_PHI , mechanism = mechanism , purview = fmt_mechanism ( ria . purview , ria . node_labels ) , direction = direction , phi = fmt_number ( ria . phi ) , partition = partition , repertoire = repertoire , partitioned_repertoire = partitioned_repertoire ) | Format a |RepertoireIrreducibilityAnalysis| . | 397 | 14 |
247,827 | def fmt_cut ( cut ) : return 'Cut {from_nodes} {symbol} {to_nodes}' . format ( from_nodes = fmt_mechanism ( cut . from_nodes , cut . node_labels ) , symbol = CUT_SYMBOL , to_nodes = fmt_mechanism ( cut . to_nodes , cut . node_labels ) ) | Format a |Cut| . | 92 | 6 |
247,828 | def fmt_sia ( sia , ces = True ) : if ces : body = ( '{ces}' '{partitioned_ces}' . format ( ces = fmt_ces ( sia . ces , 'Cause-effect structure' ) , partitioned_ces = fmt_ces ( sia . partitioned_ces , 'Partitioned cause-effect structure' ) ) ) center_header = True else : body = '' center_header = False title = 'System irreducibility analysis: {BIG_PHI} = {phi}' . format ( BIG_PHI = BIG_PHI , phi = fmt_number ( sia . phi ) ) body = header ( str ( sia . subsystem ) , body , center = center_header ) body = header ( str ( sia . cut ) , body , center = center_header ) return box ( header ( title , body , center = center_header ) ) | Format a |SystemIrreducibilityAnalysis| . | 209 | 11 |
247,829 | def fmt_repertoire ( r ) : # TODO: will this get unwieldy with large repertoires? if r is None : return '' r = r . squeeze ( ) lines = [ ] # Header: 'S P(S)' space = ' ' * 4 head = '{S:^{s_width}}{space}Pr({S})' . format ( S = 'S' , s_width = r . ndim , space = space ) lines . append ( head ) # Lines: '001 .25' for state in utils . all_states ( r . ndim ) : state_str = '' . join ( str ( i ) for i in state ) lines . append ( '{0}{1}{2}' . format ( state_str , space , fmt_number ( r [ state ] ) ) ) width = max ( len ( line ) for line in lines ) lines . insert ( 1 , DOTTED_HEADER * ( width + 1 ) ) return box ( '\n' . join ( lines ) ) | Format a repertoire . | 225 | 4 |
247,830 | def fmt_ac_ria ( ria ) : 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 ( ALPHA = ALPHA , alpha = round ( ria . alpha , 4 ) , causality = causality ) | Format an AcRepertoireIrreducibilityAnalysis . | 186 | 13 |
247,831 | def fmt_account ( account , title = None ) : if title is None : title = account . __class__ . __name__ # `Account` or `DirectedAccount` title = '{} ({} causal link{})' . format ( title , len ( account ) , '' if len ( account ) == 1 else 's' ) body = '' body += 'Irreducible effects\n' body += '\n' . join ( fmt_ac_ria ( m ) for m in account . irreducible_effects ) body += '\nIrreducible causes\n' body += '\n' . join ( fmt_ac_ria ( m ) for m in account . irreducible_causes ) return '\n' + header ( title , body , under_char = '*' ) | Format an Account or a DirectedAccount . | 179 | 9 |
247,832 | def fmt_ac_sia ( ac_sia ) : body = ( '{ALPHA} = {alpha}\n' 'direction: {ac_sia.direction}\n' 'transition: {ac_sia.transition}\n' 'before state: {ac_sia.before_state}\n' 'after state: {ac_sia.after_state}\n' 'cut:\n{ac_sia.cut}\n' '{account}\n' '{partitioned_account}' . format ( ALPHA = ALPHA , alpha = round ( ac_sia . alpha , 4 ) , ac_sia = ac_sia , account = fmt_account ( ac_sia . account , 'Account' ) , partitioned_account = fmt_account ( ac_sia . partitioned_account , 'Partitioned Account' ) ) ) return box ( header ( 'AcSystemIrreducibilityAnalysis' , body , under_char = HORIZONTAL_BAR ) ) | Format a AcSystemIrreducibilityAnalysis . | 227 | 10 |
247,833 | def fmt_transition ( t ) : return "Transition({} {} {})" . format ( fmt_mechanism ( t . cause_indices , t . node_labels ) , ARROW_RIGHT , fmt_mechanism ( t . effect_indices , t . node_labels ) ) | Format a |Transition| . | 70 | 7 |
247,834 | def direction ( direction , allow_bi = False ) : valid = [ Direction . CAUSE , Direction . EFFECT ] if allow_bi : valid . append ( Direction . BIDIRECTIONAL ) if direction not in valid : raise ValueError ( '`direction` must be one of {}' . format ( valid ) ) return True | Validate that the given direction is one of the allowed constants . | 71 | 13 |
247,835 | def tpm ( tpm , check_independence = True ) : see_tpm_docs = ( 'See the documentation on TPM conventions and the `pyphi.Network` ' 'object for more information on TPM forms.' ) # Cast to np.array. tpm = np . array ( tpm ) # Get the number of nodes from the state-by-node TPM. N = tpm . shape [ - 1 ] if tpm . ndim == 2 : if not ( ( tpm . shape [ 0 ] == 2 ** N and tpm . shape [ 1 ] == N ) or ( tpm . shape [ 0 ] == tpm . shape [ 1 ] ) ) : raise ValueError ( 'Invalid shape for 2-D TPM: {}\nFor a state-by-node TPM, ' 'there must be ' '2^N rows and N columns, where N is the ' 'number of nodes. State-by-state TPM must be square. ' '{}' . format ( tpm . shape , see_tpm_docs ) ) if tpm . shape [ 0 ] == tpm . shape [ 1 ] and check_independence : conditionally_independent ( tpm ) elif tpm . ndim == ( N + 1 ) : if tpm . shape != tuple ( [ 2 ] * N + [ N ] ) : raise ValueError ( 'Invalid shape for multidimensional state-by-node TPM: {}\n' 'The shape should be {} for {} nodes. {}' . format ( tpm . shape , ( [ 2 ] * N ) + [ N ] , N , see_tpm_docs ) ) else : raise ValueError ( 'Invalid TPM: Must be either 2-dimensional or multidimensional. ' '{}' . format ( see_tpm_docs ) ) return True | Validate a TPM . | 399 | 6 |
247,836 | def conditionally_independent ( tpm ) : if not config . VALIDATE_CONDITIONAL_INDEPENDENCE : return True tpm = np . array ( tpm ) if is_state_by_state ( tpm ) : there_and_back_again = convert . state_by_node2state_by_state ( convert . state_by_state2state_by_node ( tpm ) ) else : there_and_back_again = convert . state_by_state2state_by_node ( convert . state_by_node2state_by_state ( tpm ) ) if np . any ( ( tpm - there_and_back_again ) >= EPSILON ) : raise exceptions . ConditionallyDependentError ( 'TPM is not conditionally independent.\n' 'See the conditional independence example in the documentation ' 'for more info.' ) return True | Validate that the TPM is conditionally independent . | 197 | 11 |
247,837 | def connectivity_matrix ( cm ) : # Special case for empty matrices. if cm . size == 0 : return True if cm . ndim != 2 : raise ValueError ( "Connectivity matrix must be 2-dimensional." ) if cm . shape [ 0 ] != cm . shape [ 1 ] : raise ValueError ( "Connectivity matrix must be square." ) if not np . all ( np . logical_or ( cm == 1 , cm == 0 ) ) : raise ValueError ( "Connectivity matrix must contain only binary " "values." ) return True | Validate the given connectivity matrix . | 118 | 7 |
247,838 | def node_labels ( node_labels , node_indices ) : if len ( node_labels ) != len ( node_indices ) : raise ValueError ( "Labels {0} must label every node {1}." . format ( node_labels , node_indices ) ) if len ( node_labels ) != len ( set ( node_labels ) ) : raise ValueError ( "Labels {0} must be unique." . format ( node_labels ) ) | Validate that there is a label for each node . | 109 | 11 |
247,839 | def network ( n ) : tpm ( n . tpm ) connectivity_matrix ( n . cm ) if n . cm . shape [ 0 ] != n . size : raise ValueError ( "Connectivity matrix must be NxN, where N is the " "number of nodes in the network." ) return True | Validate a |Network| . | 67 | 7 |
247,840 | def state_length ( state , size ) : if len ( state ) != size : raise ValueError ( 'Invalid state: there must be one entry per ' 'node in the network; this state has {} entries, but ' 'there are {} nodes.' . format ( len ( state ) , size ) ) return True | Check that the state is the given size . | 66 | 9 |
247,841 | def state_reachable ( subsystem ) : # If there is a row `r` in the TPM such that all entries of `r - state` are # between -1 and 1, then the given state has a nonzero probability of being # reached from some state. # First we take the submatrix of the conditioned TPM that corresponds to # the nodes that are actually in the subsystem... tpm = subsystem . tpm [ ... , subsystem . node_indices ] # Then we do the subtraction and test. test = tpm - np . array ( subsystem . proper_state ) if not np . any ( np . logical_and ( - 1 < test , test < 1 ) . all ( - 1 ) ) : raise exceptions . StateUnreachableError ( subsystem . state ) | Return whether a state can be reached according to the network s TPM . | 167 | 15 |
247,842 | def cut ( cut , node_indices ) : if cut . indices != node_indices : raise ValueError ( '{} nodes are not equal to subsystem nodes ' '{}' . format ( cut , node_indices ) ) | Check that the cut is for only the given nodes . | 51 | 11 |
247,843 | def subsystem ( s ) : node_states ( s . state ) cut ( s . cut , s . cut_indices ) if config . VALIDATE_SUBSYSTEM_STATES : state_reachable ( s ) return True | Validate a |Subsystem| . | 51 | 8 |
247,844 | def partition ( partition ) : nodes = set ( ) for part in partition : for node in part : if node in nodes : raise ValueError ( 'Micro-element {} may not be partitioned into multiple ' 'macro-elements' . format ( node ) ) nodes . add ( node ) | Validate a partition - used by blackboxes and coarse grains . | 62 | 13 |
247,845 | def coarse_grain ( coarse_grain ) : partition ( coarse_grain . partition ) if len ( coarse_grain . partition ) != len ( coarse_grain . grouping ) : raise ValueError ( 'output and state groupings must be the same size' ) for part , group in zip ( coarse_grain . partition , coarse_grain . grouping ) : if set ( range ( len ( part ) + 1 ) ) != set ( group [ 0 ] + group [ 1 ] ) : # Check that all elements in the partition are in one of the two # state groupings raise ValueError ( 'elements in output grouping {0} do not match ' 'elements in state grouping {1}' . format ( part , group ) ) | Validate a macro coarse - graining . | 154 | 9 |
247,846 | def blackbox ( blackbox ) : if tuple ( sorted ( blackbox . output_indices ) ) != blackbox . output_indices : raise ValueError ( 'Output indices {} must be ordered' . format ( blackbox . output_indices ) ) partition ( blackbox . partition ) for part in blackbox . partition : if not set ( part ) & set ( blackbox . output_indices ) : raise ValueError ( 'Every blackbox must have an output - {} does not' . format ( part ) ) | Validate a macro blackboxing . | 111 | 7 |
247,847 | def blackbox_and_coarse_grain ( blackbox , coarse_grain ) : if blackbox is None : return for box in blackbox . partition : # Outputs of the box outputs = set ( box ) & set ( blackbox . output_indices ) if coarse_grain is None and len ( outputs ) > 1 : raise ValueError ( 'A blackboxing with multiple outputs per box must be ' 'coarse-grained.' ) if ( coarse_grain and not any ( outputs . issubset ( part ) for part in coarse_grain . partition ) ) : raise ValueError ( 'Multiple outputs from a blackbox must be partitioned into ' 'the same macro-element of the coarse-graining' ) | Validate that a coarse - graining properly combines the outputs of a blackboxing . | 154 | 17 |
247,848 | def register ( self , name ) : def register_func ( func ) : self . store [ name ] = func return func return register_func | Decorator for registering a function with PyPhi . | 30 | 12 |
247,849 | def ces ( subsystem , mechanisms = False , purviews = False , cause_purviews = False , effect_purviews = False , parallel = False ) : if mechanisms is False : mechanisms = utils . powerset ( subsystem . node_indices , nonempty = True ) engine = ComputeCauseEffectStructure ( mechanisms , subsystem , purviews , cause_purviews , effect_purviews ) return CauseEffectStructure ( engine . run ( parallel or config . PARALLEL_CONCEPT_EVALUATION ) , subsystem = subsystem ) | Return the conceptual structure of this subsystem optionally restricted to concepts with the mechanisms and purviews given in keyword arguments . | 117 | 22 |
247,850 | def conceptual_info ( subsystem ) : ci = ces_distance ( ces ( subsystem ) , CauseEffectStructure ( ( ) , subsystem = subsystem ) ) return round ( ci , config . PRECISION ) | Return the conceptual information for a |Subsystem| . | 47 | 11 |
247,851 | def evaluate_cut ( uncut_subsystem , cut , unpartitioned_ces ) : log . debug ( 'Evaluating %s...' , cut ) cut_subsystem = uncut_subsystem . apply_cut ( cut ) if config . ASSUME_CUTS_CANNOT_CREATE_NEW_CONCEPTS : mechanisms = unpartitioned_ces . mechanisms else : # Mechanisms can only produce concepts if they were concepts in the # original system, or the cut divides the mechanism. mechanisms = set ( unpartitioned_ces . mechanisms + list ( cut_subsystem . cut_mechanisms ) ) partitioned_ces = ces ( cut_subsystem , mechanisms ) log . debug ( 'Finished evaluating %s.' , cut ) phi_ = ces_distance ( unpartitioned_ces , partitioned_ces ) return SystemIrreducibilityAnalysis ( phi = phi_ , ces = unpartitioned_ces , partitioned_ces = partitioned_ces , subsystem = uncut_subsystem , cut_subsystem = cut_subsystem ) | Compute the system irreducibility for a given cut . | 242 | 13 |
247,852 | def sia_bipartitions ( nodes , node_labels = None ) : if config . CUT_ONE_APPROXIMATION : bipartitions = directed_bipartition_of_one ( nodes ) else : # Don't consider trivial partitions where one part is empty bipartitions = directed_bipartition ( nodes , nontrivial = True ) return [ Cut ( bipartition [ 0 ] , bipartition [ 1 ] , node_labels ) for bipartition in bipartitions ] | Return all |big_phi| cuts for the given nodes . | 114 | 13 |
247,853 | def _sia ( cache_key , subsystem ) : # pylint: disable=unused-argument log . info ( 'Calculating big-phi data for %s...' , subsystem ) # Check for degenerate cases # ========================================================================= # Phi is necessarily zero if the subsystem is: # - not strongly connected; # - empty; # - an elementary micro mechanism (i.e. no nontrivial bipartitions). # So in those cases we immediately return a null SIA. if not subsystem : log . info ( 'Subsystem %s is empty; returning null SIA ' 'immediately.' , subsystem ) return _null_sia ( subsystem ) if not connectivity . is_strong ( subsystem . cm , subsystem . node_indices ) : log . info ( '%s is not strongly connected; returning null SIA ' 'immediately.' , subsystem ) return _null_sia ( subsystem ) # Handle elementary micro mechanism cases. # Single macro element systems have nontrivial bipartitions because their # bipartitions are over their micro elements. if len ( subsystem . cut_indices ) == 1 : # If the node lacks a self-loop, phi is trivially zero. if not subsystem . cm [ subsystem . node_indices ] [ subsystem . node_indices ] : log . info ( 'Single micro nodes %s without selfloops cannot have ' 'phi; returning null SIA immediately.' , subsystem ) return _null_sia ( subsystem ) # Even if the node has a self-loop, we may still define phi to be zero. elif not config . SINGLE_MICRO_NODES_WITH_SELFLOOPS_HAVE_PHI : log . info ( 'Single micro nodes %s with selfloops cannot have ' 'phi; returning null SIA immediately.' , subsystem ) return _null_sia ( subsystem ) # ========================================================================= log . debug ( 'Finding unpartitioned CauseEffectStructure...' ) unpartitioned_ces = _ces ( subsystem ) if not unpartitioned_ces : log . info ( 'Empty unpartitioned CauseEffectStructure; returning null ' 'SIA immediately.' ) # Short-circuit if there are no concepts in the unpartitioned CES. return _null_sia ( subsystem ) log . debug ( 'Found unpartitioned CauseEffectStructure.' ) # TODO: move this into sia_bipartitions? # Only True if SINGLE_MICRO_NODES...=True, no? if len ( subsystem . cut_indices ) == 1 : cuts = [ Cut ( subsystem . cut_indices , subsystem . cut_indices , subsystem . cut_node_labels ) ] else : cuts = sia_bipartitions ( subsystem . cut_indices , subsystem . cut_node_labels ) engine = ComputeSystemIrreducibility ( cuts , subsystem , unpartitioned_ces ) result = engine . run ( config . PARALLEL_CUT_EVALUATION ) if config . CLEAR_SUBSYSTEM_CACHES_AFTER_COMPUTING_SIA : log . debug ( 'Clearing subsystem caches.' ) subsystem . clear_caches ( ) log . info ( 'Finished calculating big-phi data for %s.' , subsystem ) return result | Return the minimal information partition of a subsystem . | 725 | 9 |
247,854 | def _sia_cache_key ( subsystem ) : return ( hash ( subsystem ) , config . ASSUME_CUTS_CANNOT_CREATE_NEW_CONCEPTS , config . CUT_ONE_APPROXIMATION , config . MEASURE , config . PRECISION , config . VALIDATE_SUBSYSTEM_STATES , config . SINGLE_MICRO_NODES_WITH_SELFLOOPS_HAVE_PHI , config . PARTITION_TYPE , ) | The cache key of the subsystem . | 116 | 7 |
247,855 | def concept_cuts ( direction , node_indices , node_labels = None ) : for partition in mip_partitions ( node_indices , node_indices ) : yield KCut ( direction , partition , node_labels ) | Generator over all concept - syle cuts for these nodes . | 53 | 13 |
247,856 | def directional_sia ( subsystem , direction , unpartitioned_ces = None ) : if unpartitioned_ces is None : unpartitioned_ces = _ces ( subsystem ) c_system = ConceptStyleSystem ( subsystem , direction ) cuts = concept_cuts ( direction , c_system . cut_indices , subsystem . node_labels ) # Run the default SIA engine # TODO: verify that short-cutting works correctly? engine = ComputeSystemIrreducibility ( cuts , c_system , unpartitioned_ces ) return engine . run ( config . PARALLEL_CUT_EVALUATION ) | Calculate a concept - style SystemIrreducibilityAnalysisCause or SystemIrreducibilityAnalysisEffect . | 140 | 23 |
247,857 | def sia_concept_style ( subsystem ) : unpartitioned_ces = _ces ( subsystem ) sia_cause = directional_sia ( subsystem , Direction . CAUSE , unpartitioned_ces ) sia_effect = directional_sia ( subsystem , Direction . EFFECT , unpartitioned_ces ) return SystemIrreducibilityAnalysisConceptStyle ( sia_cause , sia_effect ) | Compute a concept - style SystemIrreducibilityAnalysis | 92 | 12 |
247,858 | def compute ( mechanism , subsystem , purviews , cause_purviews , effect_purviews ) : concept = subsystem . concept ( mechanism , purviews = purviews , cause_purviews = cause_purviews , effect_purviews = effect_purviews ) # Don't serialize the subsystem. # This is replaced on the other side of the queue, and ensures # that all concepts in the CES reference the same subsystem. concept . subsystem = None return concept | Compute a |Concept| for a mechanism in this |Subsystem| with the provided purviews . | 96 | 22 |
247,859 | def process_result ( self , new_concept , concepts ) : if new_concept . phi > 0 : # Replace the subsystem new_concept . subsystem = self . subsystem concepts . append ( new_concept ) return concepts | Save all concepts with non - zero |small_phi| to the |CauseEffectStructure| . | 47 | 21 |
247,860 | def process_result ( self , new_sia , min_sia ) : if new_sia . phi == 0 : self . done = True # Short-circuit return new_sia elif new_sia < min_sia : return new_sia return min_sia | Check if the new SIA has smaller |big_phi| than the standing result . | 66 | 18 |
247,861 | def concept ( self , mechanism , purviews = False , cause_purviews = False , effect_purviews = False ) : cause = self . cause_system . mic ( mechanism , purviews = ( cause_purviews or purviews ) ) effect = self . effect_system . mie ( mechanism , purviews = ( effect_purviews or purviews ) ) return Concept ( mechanism = mechanism , cause = cause , effect = effect , subsystem = self ) | Compute a concept using the appropriate system for each side of the cut . | 97 | 15 |
247,862 | def coerce_to_indices ( self , nodes ) : if nodes is None : return self . node_indices if all ( isinstance ( node , str ) for node in nodes ) : indices = self . labels2indices ( nodes ) else : indices = map ( int , nodes ) return tuple ( sorted ( set ( indices ) ) ) | Return the nodes indices for nodes where nodes is either already integer indices or node labels . | 74 | 17 |
247,863 | def _null_sia ( subsystem , phi = 0.0 ) : return SystemIrreducibilityAnalysis ( subsystem = subsystem , cut_subsystem = subsystem , phi = phi , ces = _null_ces ( subsystem ) , partitioned_ces = _null_ces ( subsystem ) ) | Return a |SystemIrreducibilityAnalysis| with zero |big_phi| and empty cause - effect structures . | 66 | 24 |
247,864 | def labeled_mechanisms ( self ) : label = self . subsystem . node_labels . indices2labels return tuple ( list ( label ( mechanism ) ) for mechanism in self . mechanisms ) | The labeled mechanism of each concept . | 42 | 7 |
247,865 | def order ( self , mechanism , purview ) : if self is Direction . CAUSE : return purview , mechanism elif self is Direction . EFFECT : return mechanism , purview from . import validate return validate . direction ( self ) | Order the mechanism and purview in time . | 49 | 9 |
247,866 | def sametype ( func ) : @ functools . wraps ( func ) def wrapper ( self , other ) : # pylint: disable=missing-docstring if type ( other ) is not type ( self ) : return NotImplemented return func ( self , other ) return wrapper | Method decorator to return NotImplemented if the args of the wrapped method are of different types . | 62 | 21 |
247,867 | def general_eq ( a , b , attributes ) : try : for attr in attributes : _a , _b = getattr ( a , attr ) , getattr ( b , attr ) if attr in [ 'phi' , 'alpha' ] : if not utils . eq ( _a , _b ) : return False elif attr in [ 'mechanism' , 'purview' ] : if _a is None or _b is None : if _a != _b : return False elif not set ( _a ) == set ( _b ) : return False else : if not numpy_aware_eq ( _a , _b ) : return False return True except AttributeError : return False | Return whether two objects are equal up to the given attributes . | 157 | 12 |
247,868 | def time_emd ( emd_type , data ) : emd = { 'cause' : _CAUSE_EMD , 'effect' : pyphi . subsystem . effect_emd , 'hamming' : pyphi . utils . hamming_emd } [ emd_type ] def statement ( ) : for ( d1 , d2 ) in data : emd ( d1 , d2 ) results = timeit . repeat ( statement , number = NUMBER , repeat = REPEAT ) return min ( results ) | Time an EMD command with the given data as arguments | 116 | 11 |
247,869 | def marginal_zero ( repertoire , node_index ) : index = [ slice ( None ) ] * repertoire . ndim index [ node_index ] = 0 return repertoire [ tuple ( index ) ] . sum ( ) | Return the marginal probability that the node is OFF . | 45 | 10 |
247,870 | def marginal ( repertoire , node_index ) : index = tuple ( i for i in range ( repertoire . ndim ) if i != node_index ) return repertoire . sum ( index , keepdims = True ) | Get the marginal distribution for a node . | 45 | 8 |
247,871 | def independent ( repertoire ) : marginals = [ marginal ( repertoire , i ) for i in range ( repertoire . ndim ) ] # TODO: is there a way to do without an explicit iteration? joint = marginals [ 0 ] for m in marginals [ 1 : ] : joint = joint * m # TODO: should we round here? # repertoire = repertoire.round(config.PRECISION) # joint = joint.round(config.PRECISION) return np . array_equal ( repertoire , joint ) | Check whether the repertoire is independent . | 110 | 7 |
247,872 | def purview ( repertoire ) : if repertoire is None : return None return tuple ( i for i , dim in enumerate ( repertoire . shape ) if dim == 2 ) | The purview of the repertoire . | 35 | 7 |
247,873 | def flatten ( repertoire , big_endian = False ) : if repertoire is None : return None order = 'C' if big_endian else 'F' # For efficiency, use `ravel` (which returns a view of the array) instead # of `np.flatten` (which copies the whole array). return repertoire . squeeze ( ) . ravel ( order = order ) | Flatten a repertoire removing empty dimensions . | 82 | 8 |
247,874 | def max_entropy_distribution ( node_indices , number_of_nodes ) : distribution = np . ones ( repertoire_shape ( node_indices , number_of_nodes ) ) return distribution / distribution . size | Return the maximum entropy distribution over a set of nodes . | 51 | 11 |
247,875 | def run_tpm ( system , steps , blackbox ) : # Generate noised TPM # Noise the connections from every output element to elements in other # boxes. node_tpms = [ ] for node in system . nodes : node_tpm = node . tpm_on for input_node in node . inputs : if not blackbox . in_same_box ( node . index , input_node ) : if input_node in blackbox . output_indices : node_tpm = marginalize_out ( [ input_node ] , node_tpm ) node_tpms . append ( node_tpm ) noised_tpm = rebuild_system_tpm ( node_tpms ) noised_tpm = convert . state_by_node2state_by_state ( noised_tpm ) tpm = convert . state_by_node2state_by_state ( system . tpm ) # Muliply by noise tpm = np . dot ( tpm , np . linalg . matrix_power ( noised_tpm , steps - 1 ) ) return convert . state_by_state2state_by_node ( tpm ) | Iterate the TPM for the given number of timesteps . | 257 | 14 |
247,876 | def _partitions_list ( N ) : if N < ( _NUM_PRECOMPUTED_PARTITION_LISTS ) : return list ( _partition_lists [ N ] ) else : raise ValueError ( 'Partition lists not yet available for system with {} ' 'nodes or more' . format ( _NUM_PRECOMPUTED_PARTITION_LISTS ) ) | Return a list of partitions of the |N| binary nodes . | 85 | 13 |
247,877 | def all_partitions ( indices ) : n = len ( indices ) partitions = _partitions_list ( n ) if n > 0 : partitions [ - 1 ] = [ list ( range ( n ) ) ] for partition in partitions : yield tuple ( tuple ( indices [ i ] for i in part ) for part in partition ) | Return a list of all possible coarse grains of a network . | 69 | 12 |
247,878 | def all_coarse_grains ( indices ) : for partition in all_partitions ( indices ) : for grouping in all_groupings ( partition ) : yield CoarseGrain ( partition , grouping ) | Generator over all possible |CoarseGrains| of these indices . | 44 | 15 |
247,879 | def all_coarse_grains_for_blackbox ( blackbox ) : for partition in all_partitions ( blackbox . output_indices ) : for grouping in all_groupings ( partition ) : coarse_grain = CoarseGrain ( partition , grouping ) try : validate . blackbox_and_coarse_grain ( blackbox , coarse_grain ) except ValueError : continue yield coarse_grain | Generator over all |CoarseGrains| for the given blackbox . | 89 | 16 |
247,880 | def all_blackboxes ( indices ) : for partition in all_partitions ( indices ) : # TODO? don't consider the empty set here # (pass `nonempty=True` to `powerset`) for output_indices in utils . powerset ( indices ) : blackbox = Blackbox ( partition , output_indices ) try : # Ensure every box has at least one output validate . blackbox ( blackbox ) except ValueError : continue yield blackbox | Generator over all possible blackboxings of these indices . | 101 | 12 |
247,881 | def coarse_graining ( network , state , internal_indices ) : max_phi = float ( '-inf' ) max_coarse_grain = CoarseGrain ( ( ) , ( ) ) for coarse_grain in all_coarse_grains ( internal_indices ) : try : subsystem = MacroSubsystem ( network , state , internal_indices , coarse_grain = coarse_grain ) except ConditionallyDependentError : continue phi = compute . phi ( subsystem ) if ( phi - max_phi ) > constants . EPSILON : max_phi = phi max_coarse_grain = coarse_grain return ( max_phi , max_coarse_grain ) | Find the maximal coarse - graining of a micro - system . | 152 | 13 |
247,882 | def all_macro_systems ( network , state , do_blackbox = False , do_coarse_grain = False , time_scales = None ) : if time_scales is None : time_scales = [ 1 ] def blackboxes ( system ) : # Returns all blackboxes to evaluate if not do_blackbox : return [ None ] return all_blackboxes ( system ) def coarse_grains ( blackbox , system ) : # Returns all coarse-grains to test if not do_coarse_grain : return [ None ] if blackbox is None : return all_coarse_grains ( system ) return all_coarse_grains_for_blackbox ( blackbox ) # TODO? don't consider the empty set here # (pass `nonempty=True` to `powerset`) for system in utils . powerset ( network . node_indices ) : for time_scale in time_scales : for blackbox in blackboxes ( system ) : for coarse_grain in coarse_grains ( blackbox , system ) : try : yield MacroSubsystem ( network , state , system , time_scale = time_scale , blackbox = blackbox , coarse_grain = coarse_grain ) except ( StateUnreachableError , ConditionallyDependentError ) : continue | Generator over all possible macro - systems for the network . | 284 | 12 |
247,883 | def emergence ( network , state , do_blackbox = False , do_coarse_grain = True , time_scales = None ) : micro_phi = compute . major_complex ( network , state ) . phi max_phi = float ( '-inf' ) max_network = None for subsystem in all_macro_systems ( network , state , do_blackbox = do_blackbox , do_coarse_grain = do_coarse_grain , time_scales = time_scales ) : phi = compute . phi ( subsystem ) if ( phi - max_phi ) > constants . EPSILON : max_phi = phi max_network = MacroNetwork ( network = network , macro_phi = phi , micro_phi = micro_phi , system = subsystem . micro_node_indices , time_scale = subsystem . time_scale , blackbox = subsystem . blackbox , coarse_grain = subsystem . coarse_grain ) return max_network | Check for the emergence of a micro - system into a macro - system . | 215 | 15 |
247,884 | def effective_info ( network ) : validate . is_network ( network ) sbs_tpm = convert . state_by_node2state_by_state ( network . tpm ) avg_repertoire = np . mean ( sbs_tpm , 0 ) return np . mean ( [ entropy ( repertoire , avg_repertoire , 2.0 ) for repertoire in sbs_tpm ] ) | Return the effective information of the given network . | 91 | 9 |
247,885 | def node_labels ( self ) : assert list ( self . node_indices ) [ 0 ] == 0 labels = list ( "m{}" . format ( i ) for i in self . node_indices ) return NodeLabels ( labels , self . node_indices ) | Return the labels for macro nodes . | 61 | 7 |
247,886 | def _squeeze ( system ) : assert system . node_indices == tpm_indices ( system . tpm ) internal_indices = tpm_indices ( system . tpm ) tpm = remove_singleton_dimensions ( system . tpm ) # The connectivity matrix is the network's connectivity matrix, with # cut applied, with all connections to/from external nodes severed, # shrunk to the size of the internal nodes. cm = system . cm [ np . ix_ ( internal_indices , internal_indices ) ] state = utils . state_of ( internal_indices , system . state ) # Re-index the subsystem nodes with the external nodes removed node_indices = reindex ( internal_indices ) nodes = generate_nodes ( tpm , cm , state , node_indices ) # Re-calcuate the tpm based on the results of the cut tpm = rebuild_system_tpm ( node . tpm_on for node in nodes ) return SystemAttrs ( tpm , cm , node_indices , state ) | Squeeze out all singleton dimensions in the Subsystem . | 235 | 13 |
247,887 | def _blackbox_partial_noise ( blackbox , system ) : # Noise inputs from non-output elements hidden in other boxes node_tpms = [ ] for node in system . nodes : node_tpm = node . tpm_on for input_node in node . inputs : if blackbox . hidden_from ( input_node , node . index ) : node_tpm = marginalize_out ( [ input_node ] , node_tpm ) node_tpms . append ( node_tpm ) tpm = rebuild_system_tpm ( node_tpms ) return system . _replace ( tpm = tpm ) | Noise connections from hidden elements to other boxes . | 139 | 10 |
247,888 | def _blackbox_time ( time_scale , blackbox , system ) : blackbox = blackbox . reindex ( ) tpm = run_tpm ( system , time_scale , blackbox ) # Universal connectivity, for now. n = len ( system . node_indices ) cm = np . ones ( ( n , n ) ) return SystemAttrs ( tpm , cm , system . node_indices , system . state ) | Black box the CM and TPM over the given time_scale . | 95 | 14 |
247,889 | def _blackbox_space ( self , blackbox , system ) : tpm = marginalize_out ( blackbox . hidden_indices , system . tpm ) assert blackbox . output_indices == tpm_indices ( tpm ) tpm = remove_singleton_dimensions ( tpm ) n = len ( blackbox ) cm = np . zeros ( ( n , n ) ) for i , j in itertools . product ( range ( n ) , repeat = 2 ) : # TODO: don't pull cm from self outputs = self . blackbox . outputs_of ( i ) to = self . blackbox . partition [ j ] if self . cm [ np . ix_ ( outputs , to ) ] . sum ( ) > 0 : cm [ i , j ] = 1 state = blackbox . macro_state ( system . state ) node_indices = blackbox . macro_indices return SystemAttrs ( tpm , cm , node_indices , state ) | Blackbox the TPM and CM in space . | 216 | 10 |
247,890 | def _coarsegrain_space ( coarse_grain , is_cut , system ) : tpm = coarse_grain . macro_tpm ( system . tpm , check_independence = ( not is_cut ) ) node_indices = coarse_grain . macro_indices state = coarse_grain . macro_state ( system . state ) # Universal connectivity, for now. n = len ( node_indices ) cm = np . ones ( ( n , n ) ) return SystemAttrs ( tpm , cm , node_indices , state ) | Spatially coarse - grain the TPM and CM . | 119 | 12 |
247,891 | def cut_mechanisms ( self ) : for mechanism in utils . powerset ( self . node_indices , nonempty = True ) : micro_mechanism = self . macro2micro ( mechanism ) if self . cut . splits_mechanism ( micro_mechanism ) : yield mechanism | The mechanisms of this system that are currently cut . | 66 | 10 |
247,892 | def apply_cut ( self , cut ) : # TODO: is the MICE cache reusable? return MacroSubsystem ( self . network , self . network_state , self . micro_node_indices , cut = cut , time_scale = self . time_scale , blackbox = self . blackbox , coarse_grain = self . coarse_grain ) | Return a cut version of this |MacroSubsystem| . | 77 | 13 |
247,893 | def potential_purviews ( self , direction , mechanism , purviews = False ) : all_purviews = utils . powerset ( self . node_indices ) return irreducible_purviews ( self . cm , direction , mechanism , all_purviews ) | Override Subsystem implementation using Network - level indices . | 58 | 10 |
247,894 | def macro2micro ( self , macro_indices ) : def from_partition ( partition , macro_indices ) : micro_indices = itertools . chain . from_iterable ( partition [ i ] for i in macro_indices ) return tuple ( sorted ( micro_indices ) ) if self . blackbox and self . coarse_grain : cg_micro_indices = from_partition ( self . coarse_grain . partition , macro_indices ) return from_partition ( self . blackbox . partition , reindex ( cg_micro_indices ) ) elif self . blackbox : return from_partition ( self . blackbox . partition , macro_indices ) elif self . coarse_grain : return from_partition ( self . coarse_grain . partition , macro_indices ) return macro_indices | Return all micro indices which compose the elements specified by macro_indices . | 186 | 15 |
247,895 | def macro2blackbox_outputs ( self , macro_indices ) : if not self . blackbox : raise ValueError ( 'System is not blackboxed' ) return tuple ( sorted ( set ( self . macro2micro ( macro_indices ) ) . intersection ( self . blackbox . output_indices ) ) ) | Given a set of macro elements return the blackbox output elements which compose these elements . | 71 | 17 |
247,896 | def micro_indices ( self ) : return tuple ( sorted ( idx for part in self . partition for idx in part ) ) | Indices of micro elements represented in this coarse - graining . | 29 | 13 |
247,897 | def reindex ( self ) : _map = dict ( zip ( self . micro_indices , reindex ( self . micro_indices ) ) ) partition = tuple ( tuple ( _map [ index ] for index in group ) for group in self . partition ) return CoarseGrain ( partition , self . grouping ) | Re - index this coarse graining to use squeezed indices . | 68 | 12 |
247,898 | def macro_state ( self , micro_state ) : assert len ( micro_state ) == len ( self . micro_indices ) # TODO: only reindex if this coarse grain is not already from 0..n? # make_mapping calls this in a tight loop so it might be more efficient # to reindex conditionally. reindexed = self . reindex ( ) micro_state = np . array ( micro_state ) return tuple ( 0 if sum ( micro_state [ list ( reindexed . partition [ i ] ) ] ) in self . grouping [ i ] [ 0 ] else 1 for i in self . macro_indices ) | Translate a micro state to a macro state | 140 | 9 |
247,899 | def make_mapping ( self ) : micro_states = utils . all_states ( len ( self . micro_indices ) ) # Find the corresponding macro-state for each micro-state. # The i-th entry in the mapping is the macro-state corresponding to the # i-th micro-state. mapping = [ convert . state2le_index ( self . macro_state ( micro_state ) ) for micro_state in micro_states ] return np . array ( mapping ) | Return a mapping from micro - state to the macro - states based on the partition and state grouping of this coarse - grain . | 106 | 25 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.