#!/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 = 50000 maxdt = 500 for dim in [9]: for eps in eps_space[:1]: path=f'/cloud/Public/_data/neuropercolation/4lay/cons=9-3dist_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: # None # if True: 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)) 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_divs = 32 pha_sort = [0]*pha_divs pha_sort = [[pha for pha in list(enumerate(phase_diff))[:-maxdt] if i*m.pi/pha_divs <= abs(pha[1]) <= (i+1)*m.pi/pha_divs] for i in range(pha_divs)] dia_ei = [np.average([ei[i] for i, pha in pha_div]) for pha_div in pha_sort] phase_dist = [len(pha)/len(phase[0][:-maxdt]) for pha in pha_sort] qtplot(f'EI Distribution against phase diff for dim={dim}', [[(div+.5)*m.pi/pha_divs for div in range(pha_divs)]], [dia_ei], [f'EI distribution against phase diff'], x_tag = 'abs phase', y_tag = 'average EI', export=True, path=path, filename=f'Phase-EI eps={round(eps,3):.3f} dim={dim} pha_divs={pha_divs}.png', close=True) qtplot(f'Phasediff Distribution for dim={dim}', [[(div+.5)*m.pi/pha_divs for div in range(pha_divs)]], [phase_dist], [f'Phase distribution'], x_tag = 'abs phase', y_tag = 'frequency', export=True, path=path, filename=f'Phasediff dist eps={round(eps,3):.3f} dim={dim} pha_divs={pha_divs}.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)