def bigmip2dict(mip, time): """Convert a BigMip to a dictionary suitable for conversion to a Matlab structure with scipy.""" if mip is None: return np.array([]) matlab_data = { 'PhiMip': mip.phi, 'main_complex': convert.nodes2indices(mip.subsystem.nodes), 'MIP1': mip.cut.intact, 'MIP2': mip.cut.severed, 'current_state': mip.subsystem.network.current_state, #'past_state': mip.subsystem.network.past_state, 'num_concepts': len(mip.unpartitioned_constellation), 'calculation_time': time, 'sum_small_phi': sum(c.phi for c in mip.unpartitioned_constellation), 'partition_only_concepts': [ concept2dict(c) for c in mip.partitioned_constellation if convert.nodes2indices(c.mechanism) not in [ convert.nodes2indices(upc.mechanism) for upc in mip.unpartitioned_constellation ] ], 'concepts': [concept2dict(c) for c in mip.unpartitioned_constellation] } return matlab_data
def concept2dict(c): """Convert a PyPhi Concept to a dictionary suitable for conversion to a Matlab structure with scipy.""" return { 'phi': c.phi, 'mechanism': convert.nodes2indices(c.mechanism), 'cause': mice2dict(c.cause) if c.cause is not None else 'None', 'effect': mice2dict(c.effect) if c.effect is not None else 'None' }
def mice2dict(mice): """Convert a PyPhi Mice to a dictionary suitable for conversion to a Matlab structure with scipy.""" return { 'phi': mice.phi, 'direction': mice.direction, 'purview': convert.nodes2indices(mice.purview), 'partition': ( ((convert.nodes2indices(mice.mip.partition[0].mechanism), convert.nodes2indices(mice.mip.partition[0].purview)), (convert.nodes2indices(mice.mip.partition[1].mechanism), convert.nodes2indices(mice.mip.partition[1].purview))) if mice.mip.partition is not None else 'None' ), 'repertoire': ( mice.mip.unpartitioned_repertoire if mice.mip.unpartitioned_repertoire is not None else 'None' ), 'partitioned_repertoire': ( mice.mip.partitioned_repertoire if mice.mip.partitioned_repertoire is not None else 'None' ) }
def check_unpartitioned_small_phis(small_phis, unpartitioned_constellation): assert len(small_phis) == len(unpartitioned_constellation) for c in unpartitioned_constellation: assert c.phi == small_phis[convert.nodes2indices(c.mechanism)]
def evaluate_animat(animat, gen, log, animat_string, matlab_results_dir): log.info("\n[Animat] " + (" Generation " + str(gen) + " ").center(60, "-")) print("running Animat: " + str(animat) + " Generation: " + str(gen) + "\n") data_path = ANIMAT_PATH + str(animat) + "_" + str(gen) + "_" #------- Make animat network for PyPhi analysis using animalysis --------------- # Animalysis takes care of all the issues with the raw data: # - Motors shouldn't connect back # - Sensors shoudn't receive feedback # - every 36 trials there is a transition which should not be counted as a causal transition try: animat_network = animalysis.Animat(data_path) if animat_network.used_nodes: # In DEBUG take only first 2 states (this is hacky, cause below l. 173 is must be more than 1 to work) if DEBUG: # TODO: not transitons but current state only now current_states = animat_network.ranked_states[0:2] else: current_states = animat_network.ranked_states # Compute results for this animat. tic = time() results = { 'animat': animat_network, 'state': tuple(filter(lambda x: x is not None, [ compute_phi_data(animat_network, state, log) for state, count in current_states ])) } #import pdb; pdb.set_trace() toc = time() elapsed = toc - tic used_nodes = tuple(animat_network.used_nodes) # We have binary nodes only num_states = 2**len(used_nodes) number_of_connections = np.count_nonzero(animat_network.cm) # Get the results in a form suitable for saving in a matfile. matlab_results = { 'tpm': animat_network.tpm.reshape([num_states]+[len(used_nodes)], order='F'), 'connectivity_matrix': animat_network.cm, 'number_of_connections': number_of_connections, 'used_nodes': used_nodes, 'LifeStates': animat_network.ranked_states, 'mip_states': [ matlab_dict.bigmip2dict(mip, t) for mip, whole, t in results['state'] ], 'whole_states': [ matlab_dict.constellation2dict(whole) for mip, whole, t in results['state'] ], 'PhiMip': [mip.phi for mip, whole, t in results['state']], #Todo: check what the result is if main complex is empty 'main_complex': [convert.nodes2indices(mip.subsystem.nodes) for mip, whole, t in results['state']], 'ssphi_whole_system': [sum(c.phi for c in whole) for mip, whole, t in results['state']], 'whole_num_concepts': [len(whole) for mip, whole, t in results['state']], 'mip_num_concepts': [len(mip.unpartitioned_constellation) for mip, whole, t in results['state']] } log.info('\n[Animat] Total time elapsed: ' + str(elapsed)) #import pdb; pdb.set_trace() # Save the matlab results in a matfile for analysis with Matlab. matfile_filename = os.path.join(matlab_results_dir, animat_string + "_" + str(gen)) scipy.io.savemat(matfile_filename, matlab_results, do_compression=True) except ValueError: print("[Animat] Animat network cannot be build.") pass