-
Notifications
You must be signed in to change notification settings - Fork 2
/
functions.py
33 lines (25 loc) · 1.02 KB
/
functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from colorsys import hls_to_rgb
import numpy as np
def colorize(z, theme = 'dark', saturation = 1., beta = 1.4, transparent = False, alpha = 1.):
r = np.abs(z)
r /= np.max(np.abs(r))
arg = np.angle(z)
h = (arg + np.pi) / (2 * np.pi) + 0.5
l = 1./(1. + r**beta) if theme == 'white' else 1.- 1./(1. + r**beta)
s = saturation
c = np.vectorize(hls_to_rgb) (h,l,s) # --> tuple
c = np.array(c) # --> array of (3,n,m) shape, but need (n,m,3)
c = c.swapaxes(0,2)
if transparent:
a = 1.-np.sum(c**2, axis = -1)/3
alpha_channel = a[...,None]**alpha
return np.concatenate([c,alpha_channel], axis = -1)
else:
return c
def complex_correlation(Y1,Y2):
Y1 = Y1-Y1.mean()
Y2 = Y2-Y2.mean()
return np.abs(np.sum(Y1.ravel() * Y2.ravel().conj())) \
/ np.sqrt(np.sum(np.abs(Y1.ravel())**2) *np.sum(np.abs(Y2.ravel())**2))
tr = lambda A,B: np.trace(np.abs(A@B.transpose().conjugate())**2)
fidelity = lambda A,B: tr(A,B)/(np.sqrt(tr(A,A)*tr(B,B)))