#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Mon Aug 21 14:59:22 2023 @author: timofej """ 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 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 H2(x): return -x*m.log2(x)-(1-x)*m.log2(1-x) extremes = None maxdt = 300 for dim in [9]: for eps in eps_space: path=f'/cloud/Public/_data/neuropercolation/4lay/cons=7-knight_steps=1000100/dim=07/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 = np.array([[np.arctan2(*act[::-1]) for act in osc[i]] for i in range(2)]) phase_diff = (phase[1]-phase[0]+m.pi)%(2*m.pi)-m.pi 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 = [np.sum(cha)*(1-H2(eps)) 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) pha_center = av_diff pha_dev = m.pi/32 last_sync = lambda i: i if abs(phase_diff[i])<0.08*m.pi else last_sync(i-1) last_orth = lambda i: i if 0.42*m.pilast_orth(i)] devm_in_ind = [i for i in dev_in_ind if phase_diff[i]<0] devp_in_ind = [i for i in dev_in_ind if phase_diff[i]>0] devm_out_ind = [i for i in dev_out_ind if phase_diff[i]<0] devp_out_ind = [i for i in dev_out_ind if phase_diff[i]>0] if extremes is None: extremes = len(dev_in_ind)//4000*1000 print(len(dev_out_ind), len(dev_in_ind)) #print(all_res, av_diff) bot_in_ind = dev_in_ind[ :extremes] top_in_ind = dev_in_ind[-extremes: ] bot_out_ind = dev_out_ind[ :extremes] top_out_ind = dev_out_ind[-extremes: ] bot_ind = bot_in_ind + bot_out_ind top_ind = top_in_ind + top_out_ind botm_in_ind = [i for i in bot_in_ind if phase_diff[i]<0] botp_in_ind = [i for i in bot_in_ind if phase_diff[i]>0] topm_in_ind = [i for i in top_in_ind if phase_diff[i]<0] topp_in_ind = [i for i in top_in_ind if phase_diff[i]>0] botm_out_ind = [i for i in bot_out_ind if phase_diff[i]<0] botp_out_ind = [i for i in bot_out_ind if phase_diff[i]>0] topm_out_ind = [i for i in top_out_ind if phase_diff[i]<0] topp_out_ind = [i for i in top_out_ind if phase_diff[i]>0] # bot_ei_pool = [ei for ei in enumerate(ei[:-maxdt]) if ei[1] <= bot_ei[1]] # top_ei_pool = [ei for ei in enumerate(ei[:-maxdt]) if ei[1] >= top_ei[1]] # bot_eis = choose(bot_ei_pool, extremes) # top_eis = choose(top_ei_pool, extremes) # bot_ind = [enum[0] for enum in bot_ei] # top_ind = [enum[0] for enum in top_ei] botm_in_res = [] topm_in_res = [] devm_in_res = [] botp_in_res = [] topp_in_res = [] devp_in_res = [] botm_out_res = [] topm_out_res = [] devm_out_res = [] botp_out_res = [] topp_out_res = [] devp_out_res = [] bot_ei = [] top_ei = [] dev_ei = [] tot_res = [] tot_ei = [] for dt in range(maxdt): botm_in_res.append( resultant([phase_diff[i+dt] for i in botm_in_ind]) ) topm_in_res.append( resultant([phase_diff[i+dt] for i in topm_in_ind]) ) devm_in_res.append( resultant([phase_diff[i+dt] for i in devm_in_ind]) ) botp_in_res.append( resultant([phase_diff[i+dt] for i in botp_in_ind]) ) topp_in_res.append( resultant([phase_diff[i+dt] for i in topp_in_ind]) ) devp_in_res.append( resultant([phase_diff[i+dt] for i in devp_in_ind]) ) botm_out_res.append( resultant([phase_diff[i+dt] for i in botm_out_ind]) ) topm_out_res.append( resultant([phase_diff[i+dt] for i in topm_out_ind]) ) devm_out_res.append( resultant([phase_diff[i+dt] for i in devm_out_ind]) ) botp_out_res.append( resultant([phase_diff[i+dt] for i in botp_out_ind]) ) topp_out_res.append( resultant([phase_diff[i+dt] for i in topp_out_ind]) ) devp_out_res.append( resultant([phase_diff[i+dt] for i in devp_out_ind]) ) bot_ei.append( np.average([ei[i+dt] for i in bot_ind]) ) top_ei.append( np.average([ei[i+dt] for i in top_ind]) ) dev_ei.append( np.average([ei[i+dt] for i in dev_ind]) ) tot_res.append( resultant(phase_diff[dt:dt-maxdt]) ) tot_ei.append( np.average(ei[dt:dt-maxdt]) ) if dt%100==99: print(f'Done dt={dt}') bm_in_res = list(zip(*botm_in_res)) tm_in_res = list(zip(*topm_in_res)) dm_in_res = list(zip(*devm_in_res)) bp_in_res = list(zip(*botp_in_res)) tp_in_res = list(zip(*topp_in_res)) dp_in_res = list(zip(*devp_in_res)) bm_out_res = list(zip(*botm_out_res)) tm_out_res = list(zip(*topm_out_res)) dm_out_res = list(zip(*devm_out_res)) bp_out_res = list(zip(*botp_out_res)) tp_out_res = list(zip(*topp_out_res)) dp_out_res = list(zip(*devp_out_res)) t_res = list(zip(*tot_res)) space = np.linspace(0,2*m.pi,101) qtplot(f'Diachronic resultant +/- for eps={round(eps,3):.3f} with dim={dim} with 4 layers', [bm_in_res[0], tm_in_res[0], dm_in_res[0], bp_in_res[0], tp_in_res[0], dp_in_res[0], bm_out_res[0], tm_out_res[0], dm_out_res[0], bp_out_res[0], tp_out_res[0], dp_out_res[0], t_res[0], np.cos(space)], [bm_in_res[1], tm_in_res[1], dm_in_res[1], bp_in_res[1], tp_in_res[1], dp_in_res[1], bm_out_res[1], tm_out_res[1], dm_out_res[1], bp_out_res[1], tp_out_res[1], dp_out_res[1], t_res[1], np.sin(space)], ['Resultant ev in of bottom {extremes} ei -', 'Resultant ev in of top {extremes} ei -', 'Resultant ev in of phase filtered ei -', 'Resultant ev in of bottom {extremes} ei +', 'Resultant ev in of top {extremes} ei +', 'Resultant ev in of phase filtered ei +', 'Resultant ev out of bottom {extremes} ei -', 'Resultant ev out of top {extremes} ei -', 'Resultant ev out of phase filtered ei -', 'Resultant ev out of bottom {extremes} ei +', 'Resultant ev out of top {extremes} ei +', 'Resultant ev out of phase filtered ei +', 'Average Resultant', 'Unit Circle'], x_tag = 'x', y_tag = 'y', lw=1, export=True, path=path, filename=f'Diachronic Resultant filtered pm inout eps={round(eps,3):.3f} dim={dim} extremes={extremes} roll{pha_dev:.3f}.png', close=True) qtplot(f'Diachronic resultant +/- for eps={round(eps,3):.3f} with dim={dim} with 4 layers', [bm_in_res[0], tm_in_res[0], dm_in_res[0], bp_in_res[0], tp_in_res[0], dp_in_res[0], t_res[0], np.cos(space)], [bm_in_res[1], tm_in_res[1], dm_in_res[1], bp_in_res[1], tp_in_res[1], dp_in_res[1], t_res[1], np.sin(space)], ['Resultant ev in of bottom {extremes} ei -', 'Resultant ev in of top {extremes} ei -', 'Resultant ev in of phase filtered ei -', 'Resultant ev in of bottom {extremes} ei +', 'Resultant ev in of top {extremes} ei +', 'Resultant ev in of phase filtered ei +', 'Average Resultant', 'Unit Circle'], x_tag = 'x', y_tag = 'y', lw=1, export=True, path=path, filename=f'Evolution of Resultant filtered pm in eps={round(eps,3):.3f} dim={dim} extremes={extremes} roll{pha_dev:.3f}.png', close=True) qtplot(f'Diachronic resultant +/- for eps={round(eps,3):.3f} with dim={dim} with 4 layers', [bm_out_res[0], tm_out_res[0], dm_out_res[0], bp_out_res[0], tp_out_res[0], dp_out_res[0], t_res[0], np.cos(space)], [bm_out_res[1], tm_out_res[1], dm_out_res[1], bp_out_res[1], tp_out_res[1], dp_out_res[1], t_res[1], np.sin(space)], ['Resultant ev out of bottom {extremes} ei -', 'Resultant ev out of top {extremes} ei -', 'Resultant ev out of phase filtered ei -', 'Resultant ev out of bottom {extremes} ei +', 'Resultant ev out of top {extremes} ei +', 'Resultant ev out of phase filtered ei +', 'Average Resultant', 'Unit Circle'], x_tag = 'x', y_tag = 'y', lw=1, export=True, path=path, filename=f'Evolution of Resultant filtered pm out eps={round(eps,3):.3f} dim={dim} extremes={extremes} roll{pha_dev:.3f}.png', close=True) # qtplot(f'Diachronic ei +/- for eps={round(eps,3):.3f} with dim={dim} with 4 layers', # [np.array(range(maxdt))]*4, # [bot_ei, top_ei, dev_ei, tot_ei], # ['EI ev of bottom {extremes} ei', 'EI ev of top {extremes} ei', # 'EI ev of phase filtered ei', 'Average EI'], # x_tag = 'dt', # y_tag = 'average ei', # export=True, # path=path, # filename=f'Diachronic EI filtered eps={round(eps,3):.3f} dim={dim} extremes={extremes} roll{pha_dev:.3f}.png', # close=True) print(f'Done eps={eps:.3f} with dim={dim} at {datetime.now()}') # qtplot(f'Resultant and EI evolution for dim={dim} with 4 layers', # [[0]+eps_space]*2, # [max(av_ei)*diff_res, av_ei], # ['Resultant', 'avEI'], # export=True, # path=path, # filename=f'Resultant and EI for dim={dim}.png', # close=True)