Ejemplo n.º 1
0
def _evaluate_cut(uncut_subsystem, cut, unpartitioned_constellation):
    """ pyphi.compute._evaluate_cut()
    
    Find the |BigMip| for a given cut."""

    cut_subsystem = Subsystem(uncut_subsystem.network,
                              uncut_subsystem.state,
                              uncut_subsystem.node_indices,
                              cut=cut,
                              mice_cache=uncut_subsystem._mice_cache)
    if config.ASSUME_CUTS_CANNOT_CREATE_NEW_CONCEPTS:
        mechanisms = set([c.mechanism for c in unpartitioned_constellation])
    else:
        mechanisms = set(
            [c.mechanism for c in unpartitioned_constellation] +
            list(cut.all_cut_mechanisms(uncut_subsystem.node_indices)))
    partitioned_constellation = constellation(cut_subsystem, mechanisms)

    phi = constellation_distance(unpartitioned_constellation,
                                 partitioned_constellation)

    return BigMip(phi=round(phi, PRECISION),
                  unpartitioned_constellation=unpartitioned_constellation,
                  partitioned_constellation=partitioned_constellation,
                  subsystem=uncut_subsystem,
                  cut_subsystem=cut_subsystem)
Ejemplo n.º 2
0
def test_find_mip_parallel_standard_example(s, flushcache, restore_fs_cache):
    flushcache()
    unpartitioned_constellation = constellation(s)
    bipartitions = utils.directed_bipartition(s.node_indices)[1:-1]
    cuts = [Cut(bipartition[0], bipartition[1])
            for bipartition in bipartitions]
    min_mip = _null_bigmip(s)
    min_mip.phi = float('inf')
    mip = _find_mip_parallel(s, cuts, unpartitioned_constellation, min_mip)
    check_mip(mip, standard_answer)
Ejemplo n.º 3
0
def test_find_mip_sequential_micro(micro_s, flushcache, restore_fs_cache):
    flushcache()

    unpartitioned_constellation = constellation(micro_s)
    bipartitions = utils.directed_bipartition(micro_s.node_indices)[1:-1]
    cuts = [Cut(bipartition[0], bipartition[1])
            for bipartition in bipartitions]
    min_mip = _null_bigmip(micro_s)
    min_mip.phi = float('inf')
    mip = _find_mip_sequential(micro_s, cuts, unpartitioned_constellation,
                               min_mip)
    check_mip(mip, micro_answer)
Ejemplo n.º 4
0
def test_find_mip_parallel_standard_example(s, flushcache, restore_fs_cache):
    flushcache()
    initial = (config.PARALLEL_CUT_EVALUATION, config.NUMBER_OF_CORES)
    config.PARALLEL_CUT_EVALUATION, config.NUMBER_OF_CORES = True, -2

    unpartitioned_constellation = constellation(s)
    bipartitions = utils.directed_bipartition(s.node_indices)[1:-1]
    cuts = [Cut(bipartition[0], bipartition[1])
            for bipartition in bipartitions]
    min_mip = _null_bigmip(s)
    min_mip.phi = float('inf')
    mip = _find_mip_parallel(s, cuts, unpartitioned_constellation, min_mip)
    check_mip(mip, standard_answer)

    config.PARALLEL_CUT_EVALUATION, config.NUMBER_OF_CORES = initial
Ejemplo n.º 5
0
def test_parallel_and_sequential_constellations_are_equal(s, micro_s, macro_s):
    with config.override(PARALLEL_CONCEPT_EVALUATION=False):
        c = compute.constellation(s)
        c_micro = compute.constellation(micro_s)
        c_macro = compute.constellation(macro_s)

    with config.override(PARALLEL_CONCEPT_EVALUATION=True):
        assert set(c) == set(compute.constellation(s))
        assert set(c_micro) == set(compute.constellation(micro_s))
        assert set(c_macro) == set(compute.constellation(macro_s))
Ejemplo n.º 6
0
def test_find_mip_parallel_micro(micro_s, flushcache, restore_fs_cache):
    flushcache()

    initial = config.PARALLEL_CUT_EVALUATION
    config.PARALLEL_CUT_EVALUATION = True

    unpartitioned_constellation = constellation(micro_s)
    bipartitions = utils.directed_bipartition(micro_s.node_indices)[1:-1]
    cuts = [Cut(bipartition[0], bipartition[1])
            for bipartition in bipartitions]
    min_mip = _null_bigmip(micro_s)
    min_mip.phi = float('inf')
    mip = _find_mip_parallel(micro_s, cuts, unpartitioned_constellation,
                             min_mip)
    check_mip(mip, micro_answer)

    config.PARALLEL_CUT_EVALUATION = initial
Ejemplo n.º 7
0
def test_find_mip_sequential_noised_example(s_noised, flushcache,
                                            restore_fs_cache):
    flushcache()
    initial = config.PARALLEL_CUT_EVALUATION
    config.PARALLEL_CUT_EVALUATION = False

    unpartitioned_constellation = constellation(s_noised)
    bipartitions = utils.directed_bipartition(s_noised.node_indices)[1:-1]
    cuts = [Cut(bipartition[0], bipartition[1])
            for bipartition in bipartitions]
    min_mip = _null_bigmip(s_noised)
    min_mip.phi = float('inf')
    mip = _find_mip_sequential(s_noised, cuts, unpartitioned_constellation, min_mip)

    check_mip(mip, noised_answer)

    config.PARALLEL_CUT_EVALUATION = initial
Ejemplo n.º 8
0
def _big_mip(cache_key, subsystem):
    """Return the minimal information partition of a subsystem.

    Args:
        subsystem (Subsystem): The candidate set of nodes.

    Returns:
        big_mip (|BigMip|): A nested structure containing all the data from the
            intermediate calculations. The top level contains the basic MIP
            information for the given subsystem.
    """
    #log.info("Calculating big-phi data for {}...".format(subsystem))
    start = time()

    if config.PARALLEL_CUT_EVALUATION:
        _find_mip = _find_mip_parallel
    else:
        _find_mip = _find_mip_sequential

    # Annote a BigMip with the total elapsed calculation time, and optionally
    # also with the time taken to calculate the unpartitioned constellation.
    def time_annotated(big_mip, small_phi_time=0.0):
        big_mip.time = time() - start
        big_mip.small_phi_time = small_phi_time
        return big_mip

    # Special case for single-node subsystems.
    if len(subsystem) == 1:
        log.info('Single-node {}; returning the hard-coded single-node MIP '
                 'immediately.'.format(subsystem))
        return time_annotated(_single_node_mip(subsystem))

    # Check for degenerate cases
    # =========================================================================
    # Phi is necessarily zero if the subsystem is:
    #   - not strongly connected;
    #   - empty; or
    #   - an elementary mechanism (i.e. no nontrivial bipartitions).
    # So in those cases we immediately return a null MIP.
    if not subsystem:
        #log.info('Subsystem {} is empty; returning null MIP '
        #'immediately.'.format(subsystem))
        return time_annotated(_null_bigmip(subsystem))

    if not utils.strongly_connected(subsystem.connectivity_matrix,
                                    subsystem.node_indices):
        #log.info('{} is not strongly connected; returning null MIP '
        #'immediately.'.format(subsystem))
        return time_annotated(_null_bigmip(subsystem))
    # =========================================================================

    #log.debug("Finding unpartitioned constellation...")
    small_phi_start = time()
    unpartitioned_constellation = constellation(subsystem)
    small_phi_time = time() - small_phi_start
    #log.debug("Found unpartitioned constellation.")

    if not unpartitioned_constellation:
        # Short-circuit if there are no concepts in the unpartitioned
        # constellation.
        result = time_annotated(_null_bigmip(subsystem))
    else:
        cuts = big_mip_bipartitions(subsystem.node_indices)
        min_mip = _null_bigmip(subsystem)
        min_mip.phi = float('inf')
        min_mip = _find_mip(subsystem, cuts, unpartitioned_constellation,
                            min_mip)
        result = time_annotated(min_mip[0], small_phi_time)

    #log.info("Finished calculating big-phi data for {}.".format(subsystem))
    #log.debug("RESULT: \n" + str(result))
    return [
        result, min_mip[1]
    ]  # results is the time-annotated MIP result in min_mip[0], min_mip[1] holds phis for all possible partition schemes
Ejemplo n.º 9
0
 def time_constellation(self, mode, network):
     clear_subsystem_caches(self.subsys)
     # network purview caches are left intact
     compute.constellation(self.subsys)
Ejemplo n.º 10
0
 def time_constellation(self, mode, network):
     clear_subsystem_caches(self.subsys)
     # network purview caches are left intact
     compute.constellation(self.subsys)