#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Mon Aug 21 14:59:22 2023 @author: astral """ 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 random import random from plot import qtplot from neuropercolation import Simulate4Layers 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 coherence(sample): phase_x = [m.cos(ind) for ind in sample] return np.average(phase_x) def bootstrap_stat(whole, sub, strength=10000, estimator='resultant'): k = len(sub) if estimator=='resultant': whole_est = norm(resultant(whole)) sub_est = norm(resultant(sub)) boot_dist = [] for i in range(strength): boot_dist.append(norm(resultant(choose(whole,k)))) if i%1000==999: print(f'Done {i:0{len(str(strength))}d} bootstraps') confidence = len([val for val in boot_dist if (val-whole_est)sub_est])/len(boot_dist) else: raise NotImplemented return confidence def bootstrap_res_parts(wholes, subs, strength=10000): ks = [len(s) for s in subs] whole = [w for whole in wholes for w in whole] whole_est = norm(resultant(whole)) resultants = [] for i in range(strength): sample = [pha for j in range(len(wholes)) for pha in choose(wholes[j],ks[j])] resultants.append(norm(resultant(sample))) if i%1000==999: print(f'Done {i:0{len(str(strength))}d} bootstraps') return resultants def new_folder(path): if not os.path.exists(path): os.makedirs(path) return 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) extremes = None maxdt = 250 stp = 1000100 batch = 0 eps_space = list(np.linspace(0.01,0.5,50)) print(f'Started at {datetime.now()}') for dim in [9]: for eps in eps_space[:1]:#list(eps_space[3:8])+list(eps_space[:3])+list(eps_space[8:]): eps = round(eps,3) path='/cloud/Public/_data/neuropercolation/4lay/cons=dimtimesdimby3_steps=100100/dim=09_cons=27/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) strength = 100000 extremes = 10000 #[l//2 for l in lens] ext_rat = extremes/(stp-100) circparts = 100 pha_dev = m.pi/circparts pha_max = np.max(np.abs(phase_diff)) phase_in_part = lambda ph, i: abs(ph)<=pha_max/circparts if i==0 else i*pha_max/circparts0 else [] for i in range(circparts)] pha_parts = [[phase_diff[i] for i in part] for part in dev_parts] syn_parts = [[phase_diff[i] for i in part] for part in top_parts] ran_sam = sample_parts(dev_parts, top_parts) top_sam = sample_parts(top_parts, top_parts) dev = [] for part in dev_parts: dev.extend(part) top = [] for part in top_parts: top.extend(part) assert sorted(top)==sorted(top_sam) ran_res = [] top_res = [] tot_res = [] bot_ei = [] top_ei = [] maxdt = 250 for dt in range(maxdt): tot_pha = phase_diff[dt:dt-maxdt] ran_pha = [(phase_diff[i+dt]) for i in ran_sam] top_pha = [(phase_diff[i+dt]) for i in top_sam] tot_res.append( norm(resultant(tot_pha)) ) ran_res.append( norm(resultant(ran_pha)) ) top_res.append( norm(resultant(top_pha)) ) sampling = 'samedist_varmaxpha' plotpath = new_folder(path+f'{sampling}_causal_roll{pha_dev:.3f}/')#'bootstrap={strength}/' savepath=new_folder(plotpath+f'extremes={extremes}_bootstrength={strength}/') newpath = new_folder(path+f'extremes={extremes}_bootstrength={strength}/') resultant_stat = bootstrap_res(phase_diff,top,strength=strength) confs = [] confdt = 250 for dt in range(confdt+1): pha_evs = [[phase_diff[i+dt] for i in part] for part in dev_parts] syn_evs = [[phase_diff[i+dt] for i in part] for part in top_parts] phas = [phase_diff[i+dt] for i in top] res_pha = norm(resultant(phas)) conf = len([val for val in resultant_stat if val