neuropercolation/evaluation/4Layer Bootstrap.py
2023-08-27 21:12:43 +02:00

148 lines
5.3 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 21 14:59:22 2023
@author: astral
"""
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=27-3diag_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 = 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/8
orth_ind = sorted([i for i,val in enumerate(ei[:-maxdt]) if (pha_center-pha_dev)<=abs(phase_diff[i])<=(pha_center+pha_dev)], key = lambda i: ei[i])
if extremes is None:
extremes = len(orth_ind)//4000*1000
print(len(orth_ind))
#print(all_res, av_diff)
bot_ind = orth_ind[ :extremes]
top_ind = orth_ind[-extremes: ]
# 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]
bot_res = []
top_res = []
orth_res = []
tot_res = []
bot_ei = []
top_ei = []
orth_ei = []
tot_ei = []
for dt in range(maxdt):
bot_res.append( norm(resultant([phase_diff[i+dt] for i in bot_ind])) )
top_res.append( norm( resultant([phase_diff[i+dt] for i in top_ind])) )
orth_res.append( norm( resultant([phase_diff[i+dt] for i in orth_ind])) )
tot_res.append( norm( resultant(phase_diff[dt:dt-maxdt])) )
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]) )
orth_ei.append( np.average([ei[i+dt] for i in orth_ind]) )
tot_ei.append( np.average(ei[dt:dt-maxdt]) )
if dt%100==99:
print(f'Done dt={dt}')
qtplot(f'Diachronic resultant for dim={dim} with 4 layers',
[np.array(range(maxdt))]*4,
[bot_res, top_res, orth_res, tot_res],
['Resultant ev of bottom {extremes} ei', 'Resultant ev of top {extremes} ei',
'Resultant ev of phase filtered ei', 'Average Resultant'],
x_tag = 'dt',
y_tag = 'concentration',
export=True,
path=path,
filename=f'Diachronic Resultant for eps={round(eps,3):.3f} dim={dim} extremes={extremes} roll{pha_dev:.3f}.png',
close=True)
qtplot(f'Diachronic ei for dim={dim} with 4 layers',
[np.array(range(maxdt))]*4,
[bot_ei, top_ei, orth_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 for 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)