#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sat Sep 9 22:23:30 2023 @author: astral """ def sampling(dim,eps,samplemethod,extremes,dtmax,noeff=1): print(f'Starting causal simulation with dim={dim}, eps={eps:.3f}, {samplemethod}, {extremes} extremes for dt={dtmax} and noeff={noeff:03d}') import os import json import math as m import numpy as np from numpy.linalg import norm from datetime import datetime from random import sample as choose from plot import qtplot from neuropercolation import Simulate4Layers eps_space = list(np.linspace(0.01,0.2,20)) def resultant(sample): phase_x = [m.cos(ind) for ind in sample] phase_y = [m.sin(ind) for ind in sample] return (np.average(phase_x), np.average(phase_y)) def new_folder(path): if not os.path.exists(path): os.makedirs(path) phase = np.vectorize(lambda x,y: (m.atan2(y,x)+m.pi)%(2*m.pi)-m.pi) diff = np.vectorize(lambda x,y: (y-x+m.pi)%(2*m.pi)-m.pi) H2 = lambda x: -x*m.log2(x)-(1-x)*m.log2(1-x) #print(f'Started at {datetime.now()} with eps={eps:.3f}') eps = round(eps,3) path='/cloud/Public/_data/neuropercolation/4lay/cons=27-knight_steps=1000100/dim=09/batch=0/' try: with open(path+f"eps={round(eps,3):.3f}_phase_diff.txt", 'r', encoding='utf-8') as f: phase_diff = json.load(f) except: with open(path+f"eps={round(eps,3):.3f}_activation.txt", 'r', encoding='utf-8') as f: activation = json.load(f)[100:] osc = list(zip(*activation)) phase_abs = np.array([[np.arctan2(*act[::-1]) for act in osc[i]] for i in range(2)]) phase_diff = diff(phase_abs[0],phase_abs[1]) phase_diff = [round(pha,6) for pha in phase_diff] with open(path+f"eps={round(eps,3):.3f}_phase_diff.txt", 'w', encoding='utf-8') as f: json.dump(list(phase_diff), f, indent=1) all_res = norm(resultant(phase_diff)) av_diff = np.arccos(all_res) try: with open(path+f"eps={round(eps,3):.3f}_ei.txt", 'r', encoding='utf-8') as f: ei = json.load(f) except: with open(path+f"eps={round(eps,3):.3f}_channels.txt", 'r', encoding='utf-8') as f: channels = json.load(f)[100:] ei = [round(np.sum(cha)*(1-H2(eps)),6) for cha in channels] with open(path+f"eps={round(eps,3):.3f}_ei.txt", 'w', encoding='utf-8') as f: json.dump(ei, f, indent=1) ei_ind = [i for i,val in enumerate(ei) if val>0] print(f'{len(ei_ind)} states with positive EI') samples = choose(ei_ind, extremes) with open(path+f"eps={round(eps,3):.3f}_states.txt", 'r', encoding='utf-8') as f: states = json.load(f)[100:] with open(path+f"eps={round(eps,3):.3f}_coupling.txt", 'r', encoding='utf-8') as f: coupling = json.load(f) coupling = [tuple(edge) for edge in coupling] phase_pairs = [[] for dt in range(dtmax+1)] ei_pairs = [[] for dt in range(dtmax+1)] for num,i in enumerate(samples): causal_init = states[i].translate(str.maketrans('', '', '.-=')) sim = Simulate4Layers( dim, eps, coupling=coupling, init=causal_init, noeffect=noeff-1, steps=dtmax, draw=None, ) activation = sim._activations() channel = sim._channels() osc = list(zip(*activation)) phase_abs = np.array([[np.arctan2(*act[::-1]) for act in osc[i]] for i in range(2)]) phasediff_c = diff(phase_abs[0],phase_abs[1]) ei_c = [round(np.sum(cha)*(1-H2(eps)),6) for cha in channel] max_ei_c = max([np.sum(cha) for cha in channel]) sim = Simulate4Layers( dim, eps, coupling=coupling, init=causal_init, noeffect=-1, steps=dtmax, draw=None, ) activation = sim._activations() channel = sim._channels() osc = list(zip(*activation)) phase_abs = np.array([[np.arctan2(*act[::-1]) for act in osc[i]] for i in range(2)]) phasediff_i = diff(phase_abs[0],phase_abs[1]) ei_i = [round(np.sum(cha)*(1-H2(eps)),6) for cha in channel] max_ei_i = max([np.sum(cha) for cha in channel]) for dt in range(1,dtmax+1): phase_pairs[dt].append((i, phasediff_i[dt], phasediff_c[dt])) ei_pairs[dt].append((i, ei_i[dt], ei_c[dt])) if num%1000==999: print(f'Done {num:0{len(str(extremes))}d}') for dt in range(1,dtmax+1): savepath = path + samplemethod + f'_samples={extremes}/dt={dt}/noeff={noeff:03d}/' new_folder(savepath) with open(savepath+f"eps={round(eps,3):.3f}_phase_pairs.txt", 'w', encoding='utf-8') as f: json.dump(phase_pairs[dt], f, indent=1) with open(savepath+f"eps={round(eps,3):.3f}_ei_pairs.txt", 'w', encoding='utf-8') as f: json.dump(ei_pairs[dt], f, indent=1) def plotting(dim,eps,samplemethod,extremes,dt): import os import json import math as m import numpy as np from numpy.linalg import norm from datetime import datetime from random import sample as choose from plot import qtplot from neuropercolation import Simulate4Layers eps_space = list(np.linspace(0.01,0.2,20)) def resultant(sample): phase_x = [m.cos(ind) for ind in sample] phase_y = [m.sin(ind) for ind in sample] return (np.average(phase_x), np.average(phase_y)) def new_folder(path): if not os.path.exists(path): os.makedirs(path) phase = np.vectorize(lambda x,y: (m.atan2(y,x)+m.pi)%(2*m.pi)-m.pi) diff = np.vectorize(lambda x,y: (y-x+m.pi)%(2*m.pi)-m.pi) H2 = lambda x: -x*m.log2(x)-(1-x)*m.log2(1-x) print(f'Started at {datetime.now()} with eps={eps:.3f}') eps = round(eps,3) path='/cloud/Public/_data/neuropercolation/4lay/cons=27-knight_steps=1000100/dim=09/batch=0/' savepath = path + samplemethod + f'_samples={extremes}/dt={dt}/' try: with open(savepath+f"eps={round(eps,3):.3f}_phase_pairs.txt", 'r', encoding='utf-8') as f: phase_pairs = json.load(f) with open(savepath+f"eps={round(eps,3):.3f}_ei_pairs.txt", 'r', encoding='utf-8') as f: ei_pairs = json.load(f) except: sampling(dim,eps,samplemethod,extremes,dt) with open(savepath+f"eps={round(eps,3):.3f}_phase_pairs.txt", 'r', encoding='utf-8') as f: phase_pairs = json.load(f) with open(savepath+f"eps={round(eps,3):.3f}_ei_pairs.txt", 'r', encoding='utf-8') as f: ei_pairs = json.load(f) t, phases_i, phases_c = zip(*phase_pairs) t, ei_i, ei_c = zip(*ei_pairs) extremes = len(t) phases_cdiff = [abs(phases_i[i])-abs(phases_c[i]) for i in range(len(t))] phase_space = np.linspace(-m.pi,m.pi,101) absph_space = np.linspace(0,m.pi,50+1) cdiff_space = np.linspace(min(phases_cdiff),max(phases_cdiff),51) phase_dist_i = [len([ph for ph in phases_i if (phase_space[j]+phase_space[j-1])/2<=ph<(phase_space[j]+phase_space[j+1])/2])/len(t) for j in range(100)] phase_dist_c = [len([ph for ph in phases_c if (phase_space[j]+phase_space[j-1])/2<=ph<(phase_space[j]+phase_space[j+1])/2])/len(t) for j in range(100)] absph_dist_i = [len([ph for ph in phases_i if low<=abs(ph)