I am working on the file "SignalProcessing/EEG Spectrogram.ipynb".
The first task is:
Spectrogram
Can you write Python code for it?

# now we will analyze window lenghts of 500 ms in 25 ms steps.
# Signals will overlap 475 ms
WinLength = int(0.5*sr) # 500 points (0.5 sec, 500 ms)
step = int(0.025*sr) # 25 points (or 25 ms)

# we have less resolution here because the signals are smaller
Nsamples = int( np.floor(WinLength/2) )
hz = np.linspace(0,Nyquist, Nsamples + 1)
dfreq = hz[1]
print('Spectral resolution = %2.4f Hz'%dfreq)

nsteps = int(np.floor ( (eeg.size - WinLength)/step) )
print(eeg.size, nsteps)

# compute manually the Fourier transform
myamp = list()
for i in range(nsteps):

    # signal duration 500 ms (512 data points)
    data = eeg[i*step:i*step+WinLength]

    FourierCoeff = np.fft.fft(data)/WinLength
    DC = [np.abs(FourierCoeff[0])] # DC component
    amp = np.concatenate((DC, 2*np.abs(FourierCoeff[1:])))

    amp = amp[:int(45/dfreq)] # only until 45 Hz
    myamp.append( amp )

hz = hz[:int(45/dfreq)]

len(myamp)


I am working on the file "SignalProcessing/Sine waves and complex waves.ipynb".
The first task is:

Complex sine waves

To have an imaginary wave, we simply need to multiply the wave by the imaginary operator, as we do with the real numbers to have the equivalent imaginary numbers. The complex wave will also have a real part, and an imaginary part. In the complex plane, the real axis is the cosine, and the imaginary axis is the sine. Thereby, we can express the complex wave as the combination of real (cosine) and imaginary (sine) parts:
$$
\cos(k) + j\sin(k)
$$
We can use Eulers formula to have this expression in its exponential form:
$$
e^{j k} = \cos(k) + j\sin(k)
$$
and subtitute $k$ by $(2 \pi \upsilon t + \phi)$.
$$
e^{j (2 \pi \upsilon t + \phi)} = \cos(2 \pi \upsilon t + \phi) + j\sin(2 \pi \upsilon t + \phi)
$$
With it, we have another convenient way to express an irrational number, because we have the angle ($k$), and the distancte to the origin ($m$).
$$
me^{j k} = m\cos(k) + mj\sin(k)
$$
Can you write Python code for it?

t = np.arange(0,np.pi, 1/30000)

freq = 2 # in Hz
phi = 0
amp = 1
k = 2*np.pi*freq*t + phi

cwv = amp * np.exp(-1j* k) # complex sine wave

fig, ax = plt.subplots(2,1, figsize=(8,4), sharex=True)
ax[0].plot(t, np.real(cwv), lw=1.5)
ax[0].plot(t, np.imag(cwv), lw=0.5, color='orange')
ax[0].set_title('real (cosine)', color='C0')

ax[1].plot(t, np.imag(cwv), color='orange', lw=1.5)
ax[1].plot(t, np.real(cwv), lw=0.5, color='C0')
ax[1].set_title('imaginary (sine)', color='orange')

for myax in ax:
    myax.set_yticks(range(-2,2,1))
    myax.set_xlabel('Time (sec)')
    myax.set_ylabel('Amplitude (AU)')


I am working on the file "SignalProcessing/Sine waves and complex waves.ipynb".
The first task is:
A complex wave is a time series of imaginary numbers. As a such, it has a real part, and an imaginary part for every time. To visualized a complex wave, we can use a 3D representation. If we plot the imaginary part vs time, we will see a sine wave, and if we plot the real part vs time, we will see a cosine wave. Looking through the real axis you will see the cosine, and through the imaginary axis the sine.
Can you write Python code for it?

from mpl_toolkits.mplot3d import Axes3D # <--- This is important for 3d plotting
fig = plt.figure()
ax = fig.gca(projection ='3d')
ax.plot(t, cwv.real, cwv.imag)
ax.set_xlabel('Time (s)'), ax.set_ylabel('Real part'), ax.set_zlabel('Imaginary part')


I am working on the file "SignalProcessing/Sine waves and complex waves.ipynb".
The first task is:
Same amplitudes and phases, but different frequencies yields to zero. Only when the steps are one or 0.5. It is
because of the sampling rate.
The same frequency but phases differe by PI over two yields zero.
Can you write Python code for it?

#
myparams1 = dict(amp = 2, freq = 5, phi = np.pi/2)
myparams2 = dict(amp = 2, freq = 5, phi = 2*np.pi/2) # ortogonal

sinew1 = mysine(t, **myparams1)
sinew2 = mysine(t, **myparams2)
fig, ax = plt.subplots(1,1, figsize=(16,4))

ax.plot(t, sinew1, lw = 2)
ax.plot(t, sinew2, color='orange', lw=2)
ax.set_ylim(-10,10)
ax.text(3, 7.5, '{:2.4f}'.format(np.dot(sinew1, sinew2)), fontsize=15);

t = np.arange(-1., 1., 1/1000.)
theta = 2*np.pi/4

morlet = lambda f : np.sin(2*np.pi*f*t + theta) * np.exp( (-t**2)/ 0.1) # Gaussian is exp(-t^2/stdev)
signal = morlet(5)

fval = np.arange(2,10,0.5)

fig, ax = plt.subplots(2,1, figsize=(16,8))

dotlist = list()
for i in fval:
    dotlist.append(np.dot(signal,morlet(i)))
    ax[0].plot(t, morlet(i), color='gray', alpha=.3)

ax[0].plot(t, signal, lw = 2)
ax[0].set_xlabel('Time (sec)')
ax[1].stem(fval, dotlist)
ax[1].set_ylabel('Dot product')


I am working on the file "SignalProcessing/Spectrogram.ipynb".
The first task is:

Generate signal

A linear-frequency chirp or simply linear chirp, the instantaneous frequency ${\displaystyle f(t)}$ varies exactly linearly with time. It is also known as a Zap input. It is a signal that sweeps through diffrent frequencies over time

Puil et al., 1986. Quantification of membrane properties of trigemenal root ganglions in guinea pigs. J. Neurophysiol. 55, 955-1016.

Can you write Python code for it?

from numpy import pi as PI

def chirp(t, f0, f1, dur):
    """
    f0 is the initial frequency
    f1 is the final frequency
    dur is the time it takes to sweep form f0 to f1
    t is the time that has elapsed from the start to the frequency ramp
    """
    return np.sin(2*PI*( f0 + (f1-f0)*t/(2*dur) )* t)

## create 5 second chirp signal with the following parameters.

# chirp between 10 and 30 Hz
sf = 1000 # sampling frequency
dt = 1/sf
time = np.arange(0,5,dt)
Nyquist = sf/2

# Define a chirp function like Udemy curse
f = (10,30) # frequencies in Hz
ff = np.linspace(f[0], np.mean(f), time.size)
signal1 = np.sin(2*PI*ff*time)

# chirp betwen 2 and 20 Hz
signal2 = chirp(time, f0=2, f1=20, dur =5)

# Fourier Transform of the first signal
fsignal1 = np.fft.fft(signal1)/signal1.size
Nsamples = int(np.floor(signal1.size/2))
hz = np.linspace(0, Nyquist, Nsamples +1)
amp1 = 2*np.abs(fsignal1)

# Fourier Transform of the second signal
fsignal2 = np.fft.fft(signal2)/signal2.size
amp2 = 2*np.abs(fsignal2)

fig, ax = plt.subplots(2,2, figsize = (16,4))#

ax[0,0].plot(time, signal1, lw = 1, color='C0')
ax[0,0].set_ylim(-1.5,1.5)
ax[0,0].set_xlabel('Time (sec)')
ax[0,0].set_title('Chirp 10 to 30 Hz')

ax[0,1].plot(time, signal2, lw =1, color='C1')
ax[0,1].set_ylim(-1.5,1.5)
ax[0,1].set_xlabel('Time (sec)')
ax[0,1].set_title('Chirp 2 to 20 Hz')

ax[1,0].plot(hz, amp1[:hz.size], color = 'C0', lw=1)
ax[1,0].set_xlim(xmin = 0, xmax =50)
ax[1,0].set_xlabel('Frequency (Hz)')

ax[1,1].plot(hz, amp2[:hz.size], color = 'C1', lw=1)
ax[1,1].set_xlim(xmin = 0, xmax =50)
ax[1,1].set_xlabel('Frequency (Hz)');

0.025*sf

# now we will analyze window lenghts of 500 ms in 25 ms steps.
# Signals will overlap 475 ms
WinLength = int(0.5*sf) # 500 points (0.5 sec, 500 ms)
step = int(0.025*sf) # 25 points (or 25 ms)

# we have less resolution here because the signals are smaller
Nsamples = int( np.floor(WinLength/2) )
hz = np.linspace(0,Nyquist, Nsamples+1)

nsteps = int(np.floor ( (signal1.size - WinLength)/step)...


I am working on the file "SignalProcessing/Spectrogram.ipynb".
The first task is:

Spectrogram of signal 1 (countour plot)

Can you write Python code for it?

# time-frequency matrix
tf = np.empty( (hz.size, nsteps ))

myamp = list()
for i in range(nsteps):

    # slice signal
    data = signal1[i*step:i*step+WinLength]

    fftcoef = np.fft.fft(data)/WinLength
    myamp.append( 2*np.abs(fftcoef[:len(hz)]) )

# plot

fig, ax = plt.subplots(2,1, figsize = (16,8), constrained_layout=True)
#fig.suptitle('Time-frequency power via short-time FFT')

ax[0].plot(time, signal1, lw = 1, color='C0')
ax[0].set_ylim(-1.5,1.5)
ax[0].set_title('Chirp 10 to 30 Hz')

# spectrum is a ContourSet object
dt = 5/180 # 5 seconds in 180 samples
X = np.arange(nsteps)*dt
Y = hz
Z = np.array(myamp).T
levels = 10
spectrum = ax[1].contourf(X,Y,Z,levels,cmap='afmhot')#,'linecolor','none')
ax[1].set_ylim([0,50])

ax[1].set_ylabel('Frequency (Hz)')

fig.colorbar(spectrum, fraction = 0.05, ticks = np.arange(0,1.2,.2))

for myax in ax:
    myax.set_xlim(0, 5)
    myax.set_xticks(np.arange(0, 5.5, 0.5))
    myax.set_xlabel('Time (sec.)')


I am working on the file "SignalProcessing/Welch's periodograms and spectrograms.ipynb".
The first task is:
A synthetic sine wave
A 120 seconds sine signal sampled at 100 Hz
Can you write Python code for it?

srate = 100
Nyquist = srate/2
print('Nysquist frequency is %d Hz'%Nyquist)
time = np.linspace(0,120,num=120*srate)
mysine = lambda f: np.sin(2*PI*f*time)

sine = 5*mysine(10)
# Plot only 5 second
fig, ax = plt.subplots(figsize=(16,4))
plt.plot(time, sine, lw = 1.5, color='C1')
plt.xlabel('Time (sec)'), plt.ylabel('Voltage ($\mu$Volts)');
plt.xlim(0,5);


I am working on the file "SignalProcessing/Welch's periodograms and spectrograms.ipynb".
The first task is:
Computing the power spectrum
The spectral power computes the power of the signal at a frequency. The spectral amplitude distribution is the series of amplitudes for each component of the oscillation. To plot the power spectrum we need to calculate first the Fourier coefficients to obtain the amplitudes or powers. To do it, the data can be taken into a number of components or segments. To obtain the power spectrum of a signal, we simply square the amplitude of the oscillation.
When computing the Fourier transforms or any spectra, it is generaly advisable to define the spectral resolution or frequency resolution.
It is the spacing between successive frequencies, and it is given by the ratio between the sampling rate and the total number of samples of the signal. Thus, at a fixed sampling

$$
\text{Spectral resolution} = \frac{\text{Sampling Rate}}{\text{Nsamples}}
$$

The Welch method computes an estimate of the power spectrum by averaging the data into overlapping segments,
The length of the segment determin the appropiate spectral resolution.

Other aspects to take into account is the tappering window to be applied, and the degree of overlap between segments.
Can you write Python code for it?


I am working on the file "SignalProcessing/Welch's periodograms and spectrograms.ipynb".
The first task is:
Example of EGG with delta band
Can you write Python code for it?

eeg = np.loadtxt('data/EEG.txt')
sr = 100 # samples per second
dt = 1/sr
time = np.arange(eeg.size)*dt

Nysquid = sr/2
print('Nysquid frequency is %d Hz'%Nysquid)
fig, ax = plt.subplots(figsize=(16,4))
plt.plot(time, eeg, lw = 1.5)
plt.xlabel('Time (sec)'), plt.ylabel('Voltage ($\mu$Volts)');

# Fourier transform
FourierCoeff = np.fft.fft(eeg)/eeg.size
DC = [np.abs(FourierCoeff[0])]
amp = np.concatenate((DC, 2*np.abs(FourierCoeff[1:])))

# compute frequencies vector until half the sampling rate
Nsamples = int( math.floor(eeg.size/2) )
hz = np.linspace(0, sr/2., num = Nsamples + 1 )
print('Spectral resolution = %2.4f Hz'%hz[1])

# Perform Welch's periodogram with hann window and 50% overlap
segment = int( 5*sr )
myhann = signal.get_window('hann', segment)

# obtain simply Power (uV^2) with Hann window and 50% overlap
myparams = dict(fs = sr, nperseg = segment, window = myhann,
                noverlap = segment/2, scaling = 'spectrum', return_onesided=True)

freq, ps = signal.welch(x = eeg, **myparams)# units uV**2
ps = 2*ps # correct for negative frequencies

# obtain Power density (uV^2/Hz) with Hann window and 50% overlap
# to get back to simply power, divide by the segment lenght in seconds (four in our case)
myparams2 = dict(fs = sr, nperseg = segment, window = myhann,
                 noverlap = segment/2, scaling = 'density', return_onesided=True)
freq, psd = signal.welch(x = eeg, **myparams2)# units uV**2
psd = 2*psd

dfreq = freq[1]
print('Spectral resolution = %2.4f Hz'%dfreq)

# Plot the power spectrum

fig, ax = plt.subplots(1, 2, figsize=(16, 4))

ax[0].set_title("Amplitude spectrum (Fourier transform)")
ax[0].stem(hz,amp[:len(hz)], use_line_collection = True)
ax[0].plot(freq, np.sqrt(ps), color='brown', lw = 2)
ax[0].set_ylabel('Amplitude ($\mu V$)')

ax[1].set_title("Power density (Welch's periodogram)")
ax[1].plot(freq, psd, color='k', lw=2)

ax[1].set_ylabel('Power spectrum density \n($\mu V^2/Hz$)')

for myax in ax:
    myax.set_xlabel('Frequency (Hz)')
    myax.set_xlim(0,20)
   ...


I am working on the file "SignalProcessing/Welch's periodograms and spectrograms.ipynb".
The first task is:
For the Welch's periodogram, the spectral resolution is given by the lenght of the segment. If we take a segment that correspond to the sampling rate, then the frequency resolution is 1 Hz. A larger segment (e.g., 4 times the size of the sampling rate) would give a higher frequency resolution (0.25 Hz).
Welch's periodogram will return the power (amplitude to the square) versus frequency. We can also have the power density. Power density is the power per Hz, and if we take a segment which is four times the sampling rate, then the power will be multiplied by four to return power/Hz. In this case, when we add in Welch's, scaling=density and will obtain the power times 4, because our segment has a lenght of four times the sampling rate.
Can you write Python code for it?

# Perform Welch's periodogram
segment = int( srate*5 )
myhann = signal.get_window('hann', segment)

# obtain simply Power (amplitude^2) withouth tappering
myparams = dict(fs = srate, nperseg = segment, window = np.ones(segment),
                noverlap = 0, scaling = 'spectrum', return_onesided=True)
freq, ps = signal.welch(x = sine, **myparams)# units uV**2
ps = 2*ps # correct for negative frequencies

#obtain Power density (amplitude^2/Hz) withouth tappering
myparams = dict(fs = srate, nperseg = segment, window = np.ones(segment),
                noverlap = 0, scaling = 'density', return_onesided=True)
freq, psd = signal.welch(x = sine, **myparams)# units uV**2/Hz
psd = 2*psd # correct for negative frequencies

dfreq = freq[1]
print('Spectral resolution = %2.4f Hz'%dfreq)

# Plot the power spectrum

fig, ax = plt.subplots(1, 2, figsize=(16, 4))

ax[0].set_title("Amplitude spectrum (Fourier transform)")
ax[0].stem(hz,amp[:len(hz)], use_line_collection = True)
ax[0].set_ylabel('Amplitude ($\mu V$)')

ax[1].set_title("Power spectrum (Welch's periodogram)")
ax[1].plot(freq, ps, color='k', lw=2)
ax[1].set_ylabel('Power spectrum ($\mu V^2$)')

for myax in ax:
    myax.set_xlabel('Frequency (Hz)')
    myax.set_xlim(0,20)
    myticks = list(range(6)) + list(range(10, 25,5))
    myax.set_xticks(myticks)

# Sine wave has an amplitude of 2 uV, the power is 4 uV and the power density is 4*5 if we took
# a segment with size = 4x the sampling rate.
print('Signal amplitude     = %2.4f uVolts'%amp[int(10/dhz)])
print('Signal power         = %2.4f uVolts^2'%ps[int(10/dfreq)])
print('Singal power density = %2.4f uVolts^2/Hz'%psd[int(10/dfreq)])

np.sqrt(124.9824/5)

# Plot spectrogram
# now we will analyze window lenghts of 500 ms
segment = int(0.5*srate) # 500 points (0.5 sec, 500 ms)

# we have less frequency resolution here because the signals are smaller
Nsamples = int( np.floor(segment/2) )
hz = np.linspace(0, Nyquist, Nsamples + 1)
dfreq = hz[1]
print('Spectral resolution = %2.4f Hz'%dfreq)

nsteps =...


I am working on the file "SignalProcessing/Welch's periodograms and spectrograms.ipynb".
The first task is:
The relative band (absolute power divided by the total power of the spectrum)
can be calculated, irrespective of if power of power densities are given in
the Welch's spectrum.
Can you write Python code for it?

# compute delta-band only for power densities!!!
idx_delta = np.logical_and(freq >= 0.4, freq <=4)
delta_power = simps(psd[idx_delta], dx = dfreq)
total_power = simps(psd, dx = dfreq)

print('Absolute delta power: %.3f uV^2' % delta_power)
print('Relative delta power: %.3f ' % (delta_power/total_power))

# With scipy
segment = int(sr)
myparams = dict(nperseg = segment, noverlap = 0, return_onesided=True, scaling='spectrum')
freq, nseg, Sxx = signal.spectrogram(x = eeg, fs=srate, **myparams)

Sxx = 2*Sxx
print('Frequency resolution = %2.4f Hz'%freq[1])

len(freq), len(nseg), len(eeg)

fig, ax = plt.subplots(2,1, figsize = (16,8), constrained_layout=True)

ax[0].plot(time, eeg, lw = 1, color='C0')
ax[0].set_ylabel('Amplitude ($\mu V$)')
ax[0].set_title('Sine wave with spectrogram form scipy')

myamp = np.sqrt(Sxx)
# spectrum is a ContourSet object
dt_seg = 1 # 120 seconds in number of steps

Z = myamp
Y = freq
X = 0.5+np.arange(len(nseg))
levels = 50
mycontour = ax[1].contourf(X,Y,Z,levels, cmap='jet')#,'linecolor','none')

# get the colormap
cbar = plt.colorbar(mycontour)#, boundaries=np.linspace(0,1,5))
cbar.ax.set_ylabel('Amplitude ($\mu$V)', rotation=90)
#cbar.set_ticks(np.arange(0,50,10))

#A working example (for any value range) with five ticks along the bar is:

m0=int(np.floor(np.min(myamp)))            # colorbar min value
m4=int(np.ceil(np.max(myamp)))             # colorbar max value
m1=int(1*(m4-m0)/4.0 + m0)               # colorbar mid value 1
m2=int(2*(m4-m0)/4.0 + m0)               # colorbar mid value 2
m3=int(3*(m4-m0)/4.0 + m0)               # colorbar mid value 3
cbar.set_ticks([m0,m1,m2,m3,m4])
cbar.set_ticklabels([m0,m1,m2,m3,m4])

#cbar.set_ticks(np.arange(0, 1.1, 0.5))

ax[1].axhline(y = 0.5, linestyle='--', linewidth = 1.5, color='white')
ax[1].axhline(y = 5, linestyle='--', linewidth = 1.5, color='white')
ax[1].set_yticks(arange(0,45,5))
ax[1].set_ylim([0,30])
ax[1].set_ylabel('Frequency (Hz)')

for myax in ax:
    myax.set_xticks(np.arange(0, 30, 5))
    myax.set_xlim(0, 30)
   ...


I am working on the file "Significant subnetworks.ipynb".
The first task is:
I2D network: 12 643 nodes, of which 10 018 are present in the data, and 142309 edges.
Can you write Python code for it?

print aces_gene_names[:10]

alist = list(aces_gene_names[:10])
gn1 = 'Entrez_5982'
gn2 = 'Entrez_76'
print alist.index(gn1)
print alist.index(gn2)

aces_gene_names = list(aces_gene_names)
edges_set = set([]) # (gene_idx_1, gene_idx_2)
# gene_idx_1 < gene_idx_2
# idx in aces_gene_names, starting at 0
with open('ACES/experiments/data/I2D_edges_0411.sif') as f:
    for line in f:
        ls = line.split()
        gene_name_1 = 'Entrez_%s' % ls[0]
        gene_name_2 = 'Entrez_%s' % ls[2]
        # Exclude self edges
        if gene_name_1 == gene_name_2:
            continue
        try:
            gene_idx_1 = aces_gene_names.index(gene_name_1)
            gene_idx_2 = aces_gene_names.index(gene_name_2)
        except ValueError:
            continue
        if gene_idx_1 < gene_idx_2:
            e = (gene_idx_1, gene_idx_2)
        else:
            e = (gene_idx_2, gene_idx_1)
        edges_set.add(e)
    f.close()

np.savetxt('I2D_edges.txt', np.array([list(x) for x in list(edges_set)]), fmt='%d')

len(edges_set)

edges_list = np.array(edges_list)

genes_in_network = set(np.array([list(x) for x in list(edges_set)]).flatten())
print len(genes_in_network)

len(set(np.where(np.sum(X_zeroed, axis=0)==0)[0]).intersection(genes_in_network))


I am working on the file "Silicon Forest Math Series | RSA.ipynb".
The first task is:
Above we see repeating cycles of numbers, with the length of the cycles all dividing 16, the totient of the prime number 17.
pow(14, 2, 17) is 9, pow(14, 3, 17) is 7, and so on, coming back around the 14 at pow(14, 17, 17) where 17 is 1 modulo 16.
Numbers raised to any kth power modulo N, where k is 1 modulo the totient of N, end up staying the same number. For example, pow(m, (n * T(N)) + 1, N) == m for any n.
Can you write Python code for it?

from random import randint

def check(N):
    totient = T(N)
    for t in totatives(N):
        n = randint(1, 10)
        print(t, pow(t, (n * totient) + 1, N))

check(17)


I am working on the file "Silicon Forest Math Series | RSA.ipynb".
The first task is:
From this totient, we'll be able to find pairs (e, d) such that (e * d) modulo T(N) == 1.
We may find d, given e and T(N), by means of the Extended Euclidean Algorithm (xgcd below).
Raising some numeric message m to the eth power modulo N will encrypt the message, giving c.
Raising the encrypted message c to the dth power will cycle it back around to its starting value, thereby decrypting it.
c = pow(m, e, N)
m = pow(c, d, N)
where (e * d) % T(N) == 1.
For example:
Can you write Python code for it?

p = 37975227936943673922808872755445627854565536638199
q = 40094690950920881030683735292761468389214899724061
RSA_100 = p * q
totient = (p - 1) * (q - 1)

# https://en.wikibooks.org/wiki/
# Algorithm_Implementation/Mathematics/
# Extended_Euclidean_algorithm

def xgcd(b, n):
    x0, x1, y0, y1 = 1, 0, 0, 1
    while n != 0:
        q, b, n = b // n, n, b % n
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return  b, x0, y0

# x = mulinv(b) mod n, (x * b) % n == 1
def mulinv(b, n):
    g, x, _ = xgcd(b, n)
    if g == 1:
        return x % n

e = 3
d = mulinv(e, totient)
print((e*d) % totient)

import binascii
m = int(binascii.hexlify(b"I'm a secret"), 16)
print(m)  # decimal encoding of byte string

c = pow(m, e, RSA_100) # raise to eth power
print(c)

m = pow(c, d, RSA_100) # raise to dth power
print(m)

binascii.unhexlify(hex(m)[2:]) # m is back where we started.


I am working on the file "Silicon Forest Math Series | RSA.ipynb".
The first task is:
How does Euclid's Method work? That's a great question and one your teacher should be able to explain. First see if you might figure it out for yourself...
Here's one explanation:
If a smaller number divides a larger one without remainder then we're done, and that will always happen when that smaller number is 1 if not before.
If there is a remainder, what then?  Lets work through an example.
81 % 18 returns a remainder of 9 in the first cycle.  18 didn't go into 81 evenly but if another smaller number goes into both 9, the remainder, and 18, then we have our answer.
9 itself does the trick and we're done.
Can you write Python code for it?

print(81 % 18) # 18 goes into
print(18 % 9)  # so the new b becomes the answer


I am working on the file "Silicon Forest Math Series | RSA.ipynb".
The first task is:
Suppose we had asked for gcd(18, 81) instead? 18 is the remainder (no 81s go into it) whereas b was 81, so the while loop simply flips the two numbers around to give the example above.
The gcd function now gives us the means to compute totients and totatives of a number.  The totatives of N are the strangers less than N, whereas the totient is the number of such strangers.
Can you write Python code for it?

def totatives(N):
    # list comprehension!
    return [x for x in range(1,N) if gcd(x,N)==1]  # strangers only

def T(N):
    """
    Returns the number of numbers between (1, N) that
    have no factors in common with N:  called the
    'totient of N' (sometimes phi is used in the docs)
    """
    return len(totatives(N)) # how many strangers did we find?

print("Totient of  100:", T(100))
print("Totient of 1000:", T(1000))


I am working on the file "Silicon Forest Math Series | RSA.ipynb".
The first task is:
Where to go next is in the direction of Euler's Theorem, a generalization of Fermat's Little Theorem.  The built-in pow(m, n, N) function will raise m to the n modulo N in an efficient manner.
Can you write Python code for it?

def powers(N):
    totient = T(N)
    print("Totient of {}:".format(N), totient)
    for t in totatives(N):
        values = [pow(t, n, N) for n in range(totient + 1)]
        cycle = values[:values.index(1, 1)]  # first 1 after initial 1
        print("{:>2}".format(len(cycle)), cycle)

powers(17)


I am working on the file "Similar image retrieval.ipynb".
The first task is:
Restore from checkpoint and calc the features from all of train data
Can you write Python code for it?

saver = tf.train.Saver()
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    ckpt = tf.train.get_checkpoint_state("model")
    saver.restore(sess, "model/model.ckpt")

    train_feat = sess.run(net, feed_dict={img_placeholder:train_images[:10000]})


I am working on the file "Similar image retrieval.ipynb".
The first task is:
Searching for similar test images from trainset based on siamese feature
Can you write Python code for it?

#generate new random test image
idx = np.random.randint(0, len_test)
im = test_images[idx]

#show the test image
show_image(idx, test_images)
print("This is image from id:", idx)

#run the test image through the network to get the test features
saver = tf.train.Saver()
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    ckpt = tf.train.get_checkpoint_state("model")
    saver.restore(sess, "model/model.ckpt")
    search_feat = sess.run(net, feed_dict={img_placeholder:[im]})

#calculate the cosine similarity and sort
dist = cdist(train_feat, search_feat, 'cosine')
rank = np.argsort(dist.ravel())

#show the top n similar image from train data
n = 7
show_image(rank[:n], train_images)
print("retrieved ids:", rank[:n])


I am working on the file "Simple Data Plots (W1 - W4 data).ipynb".
The first task is:
Load the dataset from the csv file
Can you write Python code for it?

with open('data_w1w4.csv', 'r') as f:
      reader = csv.reader(f)
      data = list(reader)


I am working on the file "Simple Data Plots (W1 - W4 data).ipynb".
The first task is:
Plot the data in 2D
Can you write Python code for it?

import matplotlib.pyplot as plt

plt.figure(1, figsize=(10, 6))
plt.xlabel('Print Time (mins)')
plt.ylabel('Filament (m)')
plt.title('Filament Use')
plt.scatter([filament], [time])

plt.figure(2, figsize=(10, 6))
plt.xlabel('Print Time (mins)')
plt.ylabel('Rating (1 - 5)')
plt.title('Rating')
plt.scatter([time], [satisfaction])

plt.figure(3, figsize=(10, 6))
plt.xlabel('Print Time (mins)')
plt.ylabel('Success / Fail / NIL')
plt.title('Result')
plt.scatter([time], [result])

plt.tight_layout()
plt.show()


I am working on the file "Simple Data Plots (W1 - W4 data).ipynb".
The first task is:
Plot the data in 3D
We are plotting filament usage against time against satisfaction rating.
Can you write Python code for it?

from mpl_toolkits.mplot3d import Axes3D

fig = plt.clf()
fig = plt.figure(1, figsize=(10, 6))
ax = fig.add_subplot(111, projection='3d')

ax.scatter([filament], [time], [satisfaction])
plt.show()


I am working on the file "Simple Data Plots (W1 - W4 data).ipynb".
The first task is:
Process the data
We will output the data in the form of a numpy matrix using our preprocessing functions.
The first row in the matrix (first data entry) and total number of samples will be displayed.
Can you write Python code for it?

matrix = obtain_data_matrix(data)
samples = len(matrix)
print("Number of samples: " + str(samples))
print(matrix[0])


I am working on the file "Simple Examples.ipynb".
The first task is:
Coin toss
We’ll repeat the example of determining the bias of a coin from observed coin tosses. The likelihood is binomial, and we use a beta prior.
Can you write Python code for it?

n = 100
h = 61
alpha = 2
beta = 2

niter = 10000
with pm.Model() as model: # context management
    # define priors
    p = pm.Beta('p', alpha=alpha, beta=beta)

    # define likelihood
    y = pm.Binomial('y', n=n, p=p, observed=h)

    # inference
    start = pm.find_MAP() # Use MAP estimate (optimization) as the initial state for MCMC
    #no need to assign manually
    #     step = pm.Metropolis() # Have a choice of samplers
    trace = pm.sample(niter, start=start, random_seed=123, progressbar=True, tune=1000, njobs=5)

with pm.Model() as model2: # context management
    # define priors, no information this time
    p = pm.Uniform('p', 0, 1)

    # define likelihood
    y = pm.Binomial('y', n=n, p=p, observed=h)

    # inference
    start = pm.find_MAP() # Use MAP estimate (optimization) as the initial state for MCMC
    #no need to assign manually
    #     step = pm.Metropolis() # Have a choice of samplers
    trace2 = pm.sample(niter, start=start, random_seed=123, progressbar=True, tune=1000, njobs=5)

pm.traceplot(trace, combined=True)
pm.traceplot(trace2, combined=True)

fig, ax = plt.subplots(ncols=2, figsize=(15,5))
sns.distplot(trace['p'], 15, label='post', ax=ax[0]);
x = np.linspace(0, 1, 100)
ax[0].plot(x, stats.beta.pdf(x, alpha, beta), label='prior');
ax[0].legend();

sns.distplot(trace2['p'], 15, label='post', ax=ax[1]);
x = np.linspace(0, 1, 100)
ax[1].plot(x, stats.uniform.pdf(x, ), label='prior');
ax[1].legend();


I am working on the file "Simple Examples.ipynb".
The first task is:
Estimating mean and standard deviation of normal distribution
Can you write Python code for it?

# generate observed data
N = 100
_mu = np.array([10])
_sigma = np.array([2])
y = np.random.normal(_mu, _sigma, N)

niter = 10000
with pm.Model() as model:
    # define priors
    mu = pm.Uniform('mu', lower=0, upper=100, shape=_mu.shape)
    sigma = pm.Uniform('sigma', lower=0, upper=10, shape=_sigma.shape)

    # define likelihood
    y_obs = pm.Normal('Y_obs', mu=mu, sd=sigma, observed=y)

    # inference
    start = pm.find_MAP()
    trace = pm.sample(niter, start=start, random_seed=123, progressbar=True, njobs=5)

pm.traceplot(trace, combined=True)

pm.summary(trace)

fig, ax = plt.subplots(ncols=2, figsize=(15,5))
sns.distplot(trace['mu'], label='post', ax=ax[0]);
x = np.linspace(np.floor(trace['mu'].min()), np.ceil(trace['mu'].max()), 100)
ax[0].plot(x, stats.uniform.pdf(x, loc=x.min(), scale=np.ptp(x)), label='prior');
ax[0].legend();

sns.distplot(trace['sigma'], label='post', ax=ax[1]);
x = np.linspace(np.floor(trace['sigma'].min()), np.ceil(trace['sigma'].max()), 100)
ax[1].plot(x, stats.uniform.pdf(x, loc=x.min(), scale=np.ptp(x)), label='prior');
ax[1].legend();
np.ptp(trace['sigma'])

ppc = pm.sample_ppc(trace, samples=500, model=model, size=100)

fig, ax = plt.subplots(ncols=2, figsize=(15,5))
sns.distplot([n.mean() for n in ppc['Y_obs']], kde=True, ax=ax[0])
ax[0].axvline(y.mean())
ax[0].set(title='Posterior predictive of the mean', xlabel='mean(x)', ylabel='Frequency');

sns.distplot([n.std() for n in ppc['Y_obs']], kde=True, ax=ax[1])
ax[1].axvline(y.std())
ax[1].set(title='Posterior predictive of the std', xlabel='std(x)', ylabel='Frequency');


I am working on the file "Simple document term matrix example.ipynb".
The first task is:
What if want to understand which words are more similar in this context?
Similarity among words
Can you write Python code for it?

similarity=cosine_similarity(document_term_matrix.T)

pd.DataFrame(similarity, index=vocab, columns=vocab)


I am working on the file "Simple-Policy.ipynb".
The first task is:
The Agent
The code below established our simple neural agent. It consists of a set of values for each of the bandit arms. Each value is an estimate of the value of the return from choosing the bandit. We use a policy gradient method to update the agent by moving the value for the selected action toward the recieved reward.
Can you write Python code for it?

tf.reset_default_graph()

#These two lines established the feed-forward part of the network.
weights = tf.Variable(tf.ones([num_arms]))
output = tf.nn.softmax(weights)

#The next six lines establish the training proceedure. We feed the reward and chosen action into the network
#to compute the loss, and use it to update the network.
reward_holder = tf.placeholder(shape=[1],dtype=tf.float32)
action_holder = tf.placeholder(shape=[1],dtype=tf.int32)

responsible_output = tf.slice(output,action_holder,[1])
loss = -(tf.log(responsible_output)*reward_holder)
optimizer = tf.train.AdamOptimizer(learning_rate=1e-3)
update = optimizer.minimize(loss)


I am working on the file "Simple-Policy.ipynb".
The first task is:
The Bandit
Here we define our bandit. For this example we are using a four-armed bandit. The pullBandit function generates a random number from a normal distribution with a mean of 0. The lower the bandit number, the more likely a positive reward will be returned. We want our agent to learn to always choose the arm that will give that positive reward.
Can you write Python code for it?

#List out our bandit arms.
#Currently arm 4 (index #3) is set to most often provide a positive reward.
bandit_arms = [0.2,0,-0.2,-2]
num_arms = len(bandit_arms)
def pullBandit(bandit):
    #Get a random number.
    result = np.random.randn(1)
    if result > bandit:
        #return a positive reward.
        return 1
    else:
        #return a negative reward.
        return -1


I am working on the file "Simple-Policy.ipynb".
The first task is:
Training the Agent
We will train our agent by taking actions in our environment, and recieving rewards. Using the rewards and actions, we can know how to properly update our network in order to more often choose actions that will yield the highest rewards over time.
Can you write Python code for it?

total_episodes = 1000 #Set total number of episodes to train agent on.
total_reward = np.zeros(num_arms) #Set scoreboard for bandit arms to 0.

init = tf.global_variables_initializer()

# Launch the tensorflow graph
with tf.Session() as sess:
    sess.run(init)
    i = 0
    while i < total_episodes:

        #Choose action according to Boltzmann distribution.
        actions = sess.run(output)
        a = np.random.choice(actions,p=actions)
        action = np.argmax(actions == a)

        reward = pullBandit(bandit_arms[action]) #Get our reward from picking one of the bandit arms.

        #Update the network.
        _,resp,ww = sess.run([update,responsible_output,weights], feed_dict={reward_holder:[reward],action_holder:[action]})

        #Update our running tally of scores.
        total_reward[action] += reward
        if i % 50 == 0:
            print("Running reward for the " + str(num_arms) + " arms of the bandit: " + str(total_reward))
        i+=1
print("\nThe agent thinks arm " + str(np.argmax(ww)+1) + " is the most promising....")
if np.argmax(ww) == np.argmax(-np.array(bandit_arms)):
    print("...and it was right!")
else:
    print("...and it was wrong!")


I am working on the file "SimpleABC_demo.ipynb".
The first task is:
Now we are ready to do some ABC. Let's get at least 100 samples from our approximate posterior, and set our tolerance (epsilon) to 0.15.
Can you write Python code for it?

posterior = basic_abc(model, data, min_samples=100, epsilon=1)


I am working on the file "SimpleABC_demo.ipynb".
The first task is:
Ok, let's see what we get.
Can you write Python code for it?

mu = posterior[0][0]

mukde = stats.gaussian_kde(mu)

plt.figure(figsize=(15,6))
plt.subplots_adjust(wspace=0.2)
plt.subplot(111)
#plt.hist(mu,normed=True)
x = np.linspace(-2,2,1000)
plt.plot(x, mukde(x))
plt.axvline(theta_0, ls="--")
plt.xlabel(r"$\mu$", fontsize=30)
plt.ylim(0,5)
plt.xlim(-2,2)


I am working on the file "SimpleABC_demo.ipynb".
The first task is:
See, not so bad at all. Ok now that we have our model class, we are ready to go. Let's set up our instance of the model object.
Can you write Python code for it?

#Set a random seed
np.random.seed(914)

#We need some real values so we know it is working.
theta_0 = 0

#Initialize our model object
model = MyModel()

#Make our "observed" data. Let's use the model's generate_data method.
data = model.generate_data(theta_0)

#Now we need to set the prior distributions. We were clever and set up our draw theta method to call
#frozen scipy.stats objects, so we jest need to give the model our prior distributions
model.set_prior([stats.norm(loc=0,scale=1)])

#And while we are at it, we will give the model our observed data as well
model.set_data(data)


I am working on the file "SimpleABC_demo.ipynb".
The first task is:
That's pretty good, but we can do better. If we make epsilon smaller, we will get a better approximation. We will use a method call population Monte Carlo (PMC) to get to a smaller tolerance and to sample from the prior more efficiently.
We'll start with what we did above, but we will take 15 PMC steps.
Can you write Python code for it?

pmc_posterior = pmc_abc(model, data, epsilon_0=1, min_samples=100, steps=15)

eps = []
for i in pmc_posterior:
    eps.append(i['epsilon'])
    mu = i[0][0]

    mukde = stats.gaussian_kde(mu)

    plt.figure(figsize=(15,3))
    plt.subplots_adjust(wspace=0.4)
    plt.suptitle(r'$\epsilon$ = {:.3f}'.format(i['epsilon']))
    plt.subplot(121)
    x = np.linspace(-2,2,1000)
    plt.plot(x, mukde(x))
    plt.axvline(theta_0, ls='--')
    plt.ylim(0,5)
    plt.xlim(-2,2)
    plt.xlabel(r"$\mu$", fontsize=30)

    plt.subplot(122)

    plt.plot(eps,'-o')
    plt.xlabel('Step', fontsize=24)
    plt.ylabel(r'$\epsilon$', fontsize=30)
    plt.xlim(-1,15)
    plt.ylim(0,1)


I am working on the file "SimpleABC_demo.ipynb".
The first task is:
The first thing we need to do is construct our model. To do this, we are going to make our own model object by subclassing SimpleABC's Model class. If you haven't been in the habit of using object-oriented programing, this might be intimidating, but it is not that hard. We need to provide an method to initialize the model object and replace the following methods(function):

draw_theta
generate_data
summary_stats
distance_function
Can you write Python code for it?

class MyModel(Model):

    #This method initializes the model object. In this case it does nothing, but you can have you model
    #do something when the model object is created, like read in a table of something.
    def __init__(self):
        pass

    #This is the method that draws from you prior. In this example it draws from frozen scipy.stats
    #distributions that are set with the Model.set_priors method.
    def draw_theta(self):
        theta = []
        for p in self.prior:
            theta.append(p.rvs())
        return theta

    #The method that generates the synthetic data sets.
    def generate_data(self, theta):
        return stats.norm.rvs(loc=theta,scale=1,size=100)

    #The method that computes your summary statistics, for a Gaussian we just need mu and sigma
    def summary_stats(self, data):
        mu = np.mean(data)

        return (mu)

    #And finally the distance function. We are just going to use the euclidean distance
    #from our observed summary stats
    def distance_function(self, data, synth_data):
        return np.abs(data - synth_data)


I am working on the file "SimpleArrayOperationTimings_Pandas.ipynb".
The first task is:
I would have expected pandas.dataFrame.sum to be more competitive with numpy.ndarray.sum, where the type of the dataFrame column was specified.
List comprehension style iteration in numpy.ndarray
Can you write Python code for it?

%timeit sum(i for i in arr[:, 2])
itersumnumpy = sum(i for i in arr[:, 2])

assert_almost_equal(itersumnumpy,arrsum, decimal=5)


I am working on the file "SimpleArrayOperationTimings_Pandas.ipynb".
The first task is:
Simple Array Operation: Sum
numpy.ndarray
Can you write Python code for it?

%timeit arr[:, 2].sum()
arrsum = arr[:, 2].sum()


I am working on the file "SimpleArrayOperationTimings_Pandas.ipynb".
The first task is:
So for a dataFame with object type, doing array operations like sum (admittedly silly), is about as good as doing this with a list comprehension. But iterating through the dataFrame rows using a list comprehension style is much worse.
Can you write Python code for it?


I am working on the file "SimpleArrayOperationTimings_Pandas.ipynb".
The first task is:
pandas.dataFrame
Can you write Python code for it?

%timeit df['z'].sum()
pdnstyle = df['z'].sum()


%timeit df.z.sum()
pdattsum = df.z.sum()

%timeit df.z.values.sum()
pdattsum = df.z.values.sum()

%timeit df.z.values.sum()
pdattsum = df.values['z'].sum()

assert_almost_equal(arrsum, pdattsum)


I am working on the file "SimpleArrayOperationTimings_Pandas.ipynb".
The first task is:
pandas.dataFrame with object type, expected to be slow
Can you write Python code for it?

df['z'] = df['z'].astype('object')

df.dtypes

%timeit df['z'].sum()
objectSum = df['z'].sum()


I am working on the file "SimpleNetworkLab.ipynb".
The first task is:
Training
Can you write Python code for it?

# TODO: Find the best parameters for each configuration
# epochs =
# batch_size =
# learning_rate =

# Gradient Descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)

# The accuracy measured against the validation set
validation_accuracy = 0.0

# Measurements use for graphing loss and accuracy
log_batch_step = 50
batches = []
loss_batch = []
train_acc_batch = []
valid_acc_batch = []

with tf.Session() as session:
    session.run(init)
    batch_count = int(math.ceil(len(train_features)/batch_size))

    for epoch_i in range(epochs):
        # The training cycle
        for batch_i in range(batch_count):
            # Get a batch of training features and labels
            batch_start = batch_i*batch_size
            batch_features = train_features[batch_start:batch_start + batch_size]
            batch_labels = train_labels[batch_start:batch_start + batch_size]

            # Run optimizer and get loss
            _, l = session.run(
                [optimizer, loss],
                feed_dict={features: batch_features, labels: batch_labels})

            # Log every 50 batches
            if not batch_i % log_batch_step:
                # Calculate Training and Validation accuracy
                training_accuracy = session.run(accuracy, feed_dict=train_feed_dict)
                validation_accuracy = session.run(accuracy, feed_dict=valid_feed_dict)

                # Log batches
                previous_batch = batches[-1] if batches else 0
                batches.append(log_batch_step + previous_batch)
                loss_batch.append(l)
                train_acc_batch.append(training_accuracy)
                valid_acc_batch.append(validation_accuracy)

        # Check accuracy against Validation data
        validation_accuracy = session.run(accuracy, feed_dict=valid_feed_dict)

loss_plot = plt.subplot(211)
loss_plot.set_title('Loss')
loss_plot.plot(batches, loss_batch, 'g')
loss_plot.set_xlim([batches[0], batches[-1]])
acc_plot =...


I am working on the file "SimpleNeuronCellTests.ipynb".
The first task is:
Create a NeuronUnit model using the cell
Can you write Python code for it?

mod1 = SingleCellModel(hVar = h, \
                       section = soma, \
                       loc = 0.5, # Current and voltage injection and measurement location on the section \
                       name = "SimpleHHCellModel")

mod1.setIntegrationMethod("fixed")  # Or "variable"
mod1.setTimeStep(1/128.0 * ms) # 2^-n are preferable
#mod1.setTolerance(0.01) # Only needed if "variable"
mod1.setStopTime(2*s)


I am working on the file "SimpleNeuronCellTests.ipynb".
The first task is:
Create a simple HH cell in NEURON
Can you write Python code for it?

soma = h.Section()
soma.L = 100
soma.diam = 100
soma.cm = 10
soma.Ra = 180
soma.insert("pas")
soma.insert("hh")


I am working on the file "SimpleNeuronCellTests.ipynb".
The first task is:
Define tests that will use NeuroElectro.org Data
Can you write Python code for it?

# Load previously saved data
import pickle

# These are saved in ExperimentalObservations notebook
with open("observations.dat", "rb") as file:
    observations = pickle.load(file)

# Define the tests to perform on the cell
testTypes = [ \
    [InputResistanceTest,            None],
    [RestingPotentialTest,           None],
    [TimeConstantTest,               {'injected_square_current': i_tau }],
    [InjectedCurrentAPWidthTest,     {'injected_square_current': i_ap }], \
    [InjectedCurrentAPThresholdTest, {'injected_square_current': i_ap }], \
    [InjectedCurrentAPAmplitudeTest, {'injected_square_current': i_ap }],
]

tests = []

# Fetch NeuroElectro property values for each test
for t in xrange(len(testTypes)):
    testType = testTypes[t][0]
    params = testTypes[t][1]

    # Create a test instance using the observations
    test = testType(observations["Pooled"][testType.name])

    if(params is not None):
        test.params = params

    tests.append(test)

# Create a test suite
suite = sciunit.TestSuite("Membrane Property Tests", tests)


I am working on the file "SimpleNeuronCellTests.ipynb".
The first task is:
Evaluate the model
Can you write Python code for it?


I am working on the file "SimpleNeuronCellTests.ipynb".
The first task is:
Evaluate the model with the test suite
Can you write Python code for it?


I am working on the file "SimpleNeuronCellTests.ipynb".
The first task is:
Set the test currents
Can you write Python code for it?

i_tau = {'amplitude': -1*nA, 'delay': 0.5*s, 'duration': 1*s}
i_ap  = {'amplitude': 20*nA, 'delay': 1*s, 'duration': 5*ms}


I am working on the file "Simple_HTTP_server.ipynb".
The first task is:
Example how to test the server with python Requests follows.
Can you write Python code for it?

import requests

parameters = {
    "a": 2,
    "b": 3,
    "c": 4,
}

r = requests.post("http://127.0.0.1:8000/", params=parameters)

print r.status_code, r.text


I am working on the file "Simple_HTTP_server.ipynb".
The first task is:
If you want make your server accesible from other computers, change the Address for your IP address. You can find your IP address for example here.
More usable example
In this example is shown custom handler. The handler reponds to some urls ('/', '/help') with status 200. For any other url the server returns status 404 - page not found.
Can you write Python code for it?

import SimpleHTTPServer
import SocketServer

class MyRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):

    def do_GET(self):
        """Respond to a GET request."""
        if self.path == "/":
            self.send_response(200)
            self.send_header("Content-type", "text/html")
            self.end_headers()
            message = "This is the main page."
        elif self.path in ["/help/", "/help"]:
            self.send_response(200)
            self.send_header("Content-type", "text/html")
            self.end_headers()
            message = "This is page with help."
        else:
            self.send_response(404)
            self.send_header("Content-type", "text/html")
            self.end_headers()
            message = "Page not found. See /help/ for help."
        # write message
        self.wfile.write(message)

PORT = 8000
ADDRESS = "127.0.0.1"

httpd = SocketServer.TCPServer((ADDRESS, PORT), MyRequestHandler)

print "Serving at port", PORT
httpd.serve_forever()


I am working on the file "Simple_HTTP_server.ipynb".
The first task is:
POST parameters handling
Example with custom POST handler follows. This server accept any url and process the POST request parameters. If you access this server with browser you will get simple HTML form that you can use to test POST request.
Can you write Python code for it?

import SimpleHTTPServer
import SocketServer
import cgi
import urlparse

class MyRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):

    def do_POST(self):
        # get params from request
        if "?" in self.path:
            # get params sent in url
            qs = self.path.split("?")[1]
            query_dict = dict(urlparse.parse_qsl(qs))
        else:
            # get params send as data
            form = cgi.FieldStorage(
                fp=self.rfile,
                headers=self.headers,
                environ={'REQUEST_METHOD':'POST',
                         'CONTENT_TYPE':self.headers['Content-Type'],
                })
            query_dict = {key: form.getvalue(key) for key in form.keys()}
        # prepare response
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        self.wfile.write("Parameters sent:"+str(query_dict))

    def do_GET(self):
        """Respond to a GET request."""
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        html_form = """
        <form method="post" action="/">
          <input type="text" name="a" value="1"><br>
          <input type="submit" value="Send">
        </form>
        """
        self.wfile.write(html_form)

PORT = 8000
ADDRESS = "127.0.0.1"

Handler = MyRequestHandler

httpd = SocketServer.TCPServer((ADDRESS, PORT), Handler)

print "Serving at port", PORT
httpd.serve_forever()


I am working on the file "Simple_HTTP_server.ipynb".
The first task is:
To test such a server you can open in your browser this page http://127.0.0.1:8000/ or you can run following code (but not in this notebook!).
Can you write Python code for it?

import requests

r = requests.get("http://127.0.0.1:8000/")

print(r.status_code)
print(r.text)


I am working on the file "Simple_Neural_Network_example.ipynb".
The first task is:
Checking out the data
This dataset has the number of riders for each hour of each day from January 1 2011 to December 31 2012. The number of riders is split between casual and registered, summed up in the cnt column. You can see the first few rows of the data above.
Below is a plot showing the number of bike riders over the first 10 days in the data set. You can see the hourly rentals here. This data is pretty complicated! The weekends have lower over all ridership and there are spikes when people are biking to and from work during the week. Looking at the data above, we also have information about temperature, humidity, and windspeed, all of these likely affecting the number of riders. You'll be trying to capture all this with your model.
Can you write Python code for it?


I am working on the file "Simple_Neural_Network_example.ipynb".
The first task is:
Load and prepare the data
A critical step in working with neural networks is preparing the data correctly. Variables on different scales make it difficult for the network to efficiently learn the correct weights. Below, we've written the code to load and prepare the data. You'll learn more about this soon!
Can you write Python code for it?

data_path = 'Bike-Sharing-Dataset/hour.csv'

rides = pd.read_csv(data_path)

rides.head()


I am working on the file "Simple_Neural_Network_example.ipynb".
The first task is:
Thinking about your results
Answer these questions about your results. How well does the model predict the data? Where does it fail? Why does it fail where it does?

Note: You can edit the text in this cell by double clicking on it. When you want to render the text, press control + enter

Your answer below
Unit tests
Run these unit tests to check the correctness of your network implementation. These tests must all be successful to pass the project.
Can you write Python code for it?

import unittest

inputs = [0.5, -0.2, 0.1]
targets = [0.4]
test_w_i_h = np.array([[0.1, 0.4, -0.3],
                       [-0.2, 0.5, 0.2]])
test_w_h_o = np.array([[0.3, -0.1]])

class TestMethods(unittest.TestCase):

    ##########
    # Unit tests for data loading
    ##########

    def test_data_path(self):
        # Test that file path to dataset has been unaltered
        self.assertTrue(data_path.lower() == 'bike-sharing-dataset/hour.csv')

    def test_data_loaded(self):
        # Test that data frame loaded
        self.assertTrue(isinstance(rides, pd.DataFrame))

    ##########
    # Unit tests for network functionality
    ##########

    def test_activation(self):
        network = NeuralNetwork(3, 2, 1, 0.5)
        # Test that the activation function is a sigmoid
        self.assertTrue(np.all(network.activation_function(0.5) == 1/(1+np.exp(-0.5))))

    def test_train(self):
        # Test that weights are updated correctly on training
        network = NeuralNetwork(3, 2, 1, 0.5)
        network.weights_input_to_hidden = test_w_i_h.copy()
        network.weights_hidden_to_output = test_w_h_o.copy()

        network.train(inputs, targets)
        self.assertTrue(np.allclose(network.weights_hidden_to_output,
                                    np.array([[ 0.37275328, -0.03172939]])))
        self.assertTrue(np.allclose(network.weights_input_to_hidden,
                                    np.array([[ 0.10562014,  0.39775194, -0.29887597],
                                              [-0.20185996,  0.50074398,  0.19962801]])))

    def test_run(self):
        # Test correctness of run method
        network = NeuralNetwork(3, 2, 1, 0.5)
        network.weights_input_to_hidden = test_w_i_h.copy()
        network.weights_hidden_to_output = test_w_h_o.copy()

        self.assertTrue(np.allclose(network.run(inputs), 0.09998924))

suite = unittest.TestLoader().loadTestsFromModule(TestMethods())
unittest.TextTestRunner().run(suite)


I am working on the file "Simple_Neural_Network_example.ipynb".
The first task is:
Time to build the network
Below you'll build your network. We've built out the structure and the backwards pass. You'll implement the forward pass through the network. You'll also set the hyperparameters: the learning rate, the number of hidden units, and the number of training passes.
The network has two layers, a hidden layer and an output layer. The hidden layer will use the sigmoid function for activations. The output layer has only one node and is used for the regression, the output of the node is the same as the input of the node. That is, the activation function is $f(x)=x$. A function that takes the input signal and generates an output signal, but takes into account the threshold, is called an activation function. We work through each layer of our network calculating the outputs for each neuron. All of the outputs from one layer become inputs to the neurons on the next layer. This process is called forward propagation.
We use the weights to propagate signals forward from the input to the output layers in a neural network. We use the weights to also propagate error backwards from the output back into the network to update our weights. This is called backpropagation.

Hint: You'll need the derivative of the output activation function ($f(x) = x$) for the backpropagation implementation. If you aren't familiar with calculus, this function is equivalent to the equation $y = x$. What is the slope of that equation? That is the derivative of $f(x)$.

Below, you have these tasks:
1. Implement the sigmoid function to use as the activation function. Set self.activation_function in __init__ to your sigmoid function.
2. Implement the forward pass in the train method.
3. Implement the backpropagation algorithm in the train method, including calculating the output error.
4. Implement the forward pass in the run method.
Can you write Python code for it?

class NeuralNetwork(object):
    def __init__(self, input_nodes, hidden_nodes, output_nodes, learning_rate):
        # Set number of nodes in input, hidden and output layers.
        self.input_nodes = input_nodes
        self.hidden_nodes = hidden_nodes
        self.output_nodes = output_nodes

        # Initialize weights
        self.weights_input_to_hidden = np.random.normal(0.0, self.hidden_nodes**-0.5,
                                       (self.hidden_nodes, self.input_nodes))
        self.weights_hidden_to_output = np.random.normal(0.0, self.output_nodes**-0.5,
                                       (self.output_nodes, self.hidden_nodes))
        self.lr = learning_rate

        #### Set this to your implemented sigmoid function ####
        # Activation function is the sigmoid function
        self.activation_function = lambda x: 1 / ( 1 + np.exp(-x))
        self.derivative = lambda sig: sig * (1-sig)

    def train(self, inputs_list, targets_list):
        # Convert inputs list to 2d array
        inputs = np.array(inputs_list, ndmin=2).T
        targets = np.array(targets_list, ndmin=2).T
        #### Implement the forward pass here ####
        ### Forward pass ###
        # TODO: Hidden layer
        hidden_inputs = np.dot(self.weights_input_to_hidden, inputs) # signals into hidden layer
        # => [ 2 x 1]
        hidden_outputs = self.activation_function(hidden_inputs)  # signals from hidden layer
        # => [ 2 x 1]

        # TODO: Output layer
        #                                 [1 x 2]               [ 2 x 1]
        final_inputs = np.dot(self.weights_hidden_to_output, hidden_outputs) # signals into final output layer
        # => [ 1 x 1]
        final_outputs = final_inputs  # signals from final output layer
        # => [1 x 1]
        #### Implement the backward pass here ####
        ### Backward pass ###

        # TODO: Output error
        #              [1 x 1]    -   [1 x 1]
        output_errors = targets - final_outputs #...


I am working on the file "Simple_Neural_Network_example.ipynb".
The first task is:
We'll split the data into two sets, one for training and one for validating as the network is being trained. Since this is time series data, we'll train on historical data, then try to predict on future data (the validation set).
Can you write Python code for it?

# Hold out the last 60 days of the remaining data as a validation set
train_features, train_targets = features[:-60*24], targets[:-60*24]
val_features, val_targets = features[-60*24:], targets[-60*24:]


I am working on the file "Simple_root_finding.ipynb".
The first task is:
Analysis shows that near a simple root,  Newton's method converges quadratically i.e.
$$|e_{n+1}| = C |e_n|^2$$
thus doubling the number of significant digits per iteration. This analysis is only valid near the root and in general, Newton's method can be highly unstable (for example if it finds a region where $f'(x)$ is close to zero), and in general requires some additional controls to maintain a bracket.
Comparing the two methods for this problem, however, shows that Newton's method converges quadratically,  while the fixed point iteration converges linearly
Can you write Python code for it?

plt.figure()
plt.semilogy(range(len(xn)),numpy.abs(f(xn)),'b-o',label='fixed point')
plt.hold(True)
plt.semilogy(range(len(xnn)),numpy.abs(f(xnn)),'r-o',label='newton')
plt.xlabel('Iterations')
plt.ylabel('Residual $|f(x)|$')
plt.legend(loc='best')
plt.title('Comparison of Fixed point iteration to Newtons Method')
plt.show()


I am working on the file "Simple_root_finding.ipynb".
The first task is:
Newton's Method
A  potentially more efficient method for Non-linear problems is Newton's method which can be considered another fixed-point iteration but promises much better convergence (near the fixed point, for simple roots).
The basic idea is that given some initial guess $x_n$ such that $f(x_n) \neq 0$, there is some correction $\delta_n$ such that $f(x_n + \delta_n) = 0$.  Expanding $f$ in a Taylor series around $x_n$ we get the linear approximation
$$ f(x_n + \delta_n) \approx f(x_n) + f'(x_n)\delta_n + O(\delta_n^2) = 0$$
neglecting terms of order $\delta_n^2$ we can solve for the correction that would be exact if the problem were linear, i.e.
$$ \delta_n = -f(x_n)/f'(x_n) $$
then the next iterate is given by
$$ x_{n+1} = x_{n} + \delta_n $$
and iterate until the residual $|f(x)| < \mathrm{tol}$ for some tolerance.
Algorithmically...
Can you write Python code for it?

fprime = lambda x: 1. + numpy.sin(x)

xnn = numpy.zeros(10)
print "\nNewton's Method\n"
i = 0
tol = 1.e-16
while numpy.abs(f(xnn[i])) > tol:
    print "step {0}: x = {1}, residual f(x) = {2}".format(i,xnn[i], f(xnn[i]))
    xnn[i+1] = xnn[i] - f(xnn[i])/fprime(xnn[i])
    i += 1

imax = i
xnn = xnn[:imax]


I am working on the file "Simple_root_finding.ipynb".
The first task is:
Root Finding
GOAL: Find where $f(x) = 0$.
Example:  A simple non-linear problem
let $f(x) = x - \cos(x)$,  find  values of $x$ where $f(x)=0$.
Because $f$ is non-linear it is possible that it has no roots, a finite number of roots or even an infinite number of roots.  Thus the first thing one should do is try to visualize $f$ over a range of $x$ to see, qualitatively, whether there are any zero crossings and to identify brackets where $f$ changes sign.
Here we will introduce our function using an "inlined-function" or "lambda function" in python
Can you write Python code for it?


I am working on the file "Simple_root_finding.ipynb".
The first task is:
Which, in this range, has a single root somewhere in the bracket $0<x<5$.  The question is how to find it?
The "right answer"
The correct approach is to use (and understand) a good algorithm for bracketed root finding of functions of a single variable of which scipy.optimize provides several.  Here we will use the brentq algorithm which is a workhorse for rootfinding as it is guaranteed to find at least one root given a proper bracket (where $f$ changes sign).  The method is a generalized secant method so doesn't require derivatives of $f$ and has super-linear convergence (a simple bisection scheme also guarantees a root but has only linear convergence).
Can you write Python code for it?

from scipy.optimize import brentq

# give a bracket [a,b] such that f(a)*f(b) <= 0
a = 0.
b = 5.
x0 = brentq(f,a,b)
print
print "root x0 = {0}, in bracket [{1},{2}]".format(x0,a,b)
print "residual f(x0) = {0}".format(f(x0))

plt.figure()
plt.plot(x,f(x),'b')
plt.hold(True)
plt.plot(x,numpy.zeros(x.shape),'r--')
plt.plot(x0,f(x0),'go')
plt.xlabel("x")
plt.ylabel("f(x)")
plt.title("A root at $x_0={0}$".format(x0))
plt.show()


I am working on the file "Simple_root_finding.ipynb".
The first task is:
and numerically by comparing the ratio of $|f(x_{n+1})|/|f(x_n)|$
Can you write Python code for it?

print
for i in range(len(xn)-1):
    print 'Step = {0}, K={1}'.format((i+1),numpy.abs(f(xn[i+1]))/numpy.abs(f(xn[i])))

gprime = lambda x: -numpy.sin(x)
print
print "|g'(x0)| = {0}".format(numpy.abs(gprime(x0)))


I am working on the file "Simple_root_finding.ipynb".
The first task is:
and plot out the residual.
Can you write Python code for it?

plt.figure()
plt.plot(range(len(xn)),f(xn),'b-o')
plt.xlabel('Iterations')
plt.ylabel('Residual $f(x)$')
plt.title('Convergence of fixed point iteration)')
plt.show()


I am working on the file "Simple_root_finding.ipynb".
The first task is:
or let's plot this for $x\in [-10,10]$
Can you write Python code for it?

x = numpy.linspace(-10.,10., 100)

plt.figure()
plt.plot(x,f(x),'b')
plt.hold(True)
plt.plot(x,numpy.zeros(x.shape),'r--')
plt.xlabel("x")
plt.ylabel("f(x)")
plt.title("A plot")
plt.show()


I am working on the file "Simple_root_finding.ipynb".
The first task is:
which simply replaces any use of f(x) with x - cos(x), for example
Can you write Python code for it?


I am working on the file "SimplexAlgorithm.ipynb".
The first task is:
Given the  following optimization problem
$\hspace{2.7cm} \min c^T\textbf{x}$
subject to $\hspace{.1cm} A_{eq}\textbf{x} = \textbf{b}_{eq}$
$\hspace{2.7cm} A_u \textbf{x} \leq \textbf{b}_u$
$\hspace{3.5cm}  \textbf{x} \geq \textbf{0}$
The simplex algorithm as class object will be initialize with
c: cost vector $c^T$
A: $A_{eq}$
b: $b_{eq}$
A_l: $A_{u}$
b_l: $b_{u}$
Can you write Python code for it?

class SimplexProblem():

    def __init__(self, c=None, A=None, b=None, A_l=None, b_l = None):

        if (A_l is None and  not (A is None) ):

            self.b = b
            self.artf_num = A.shape[0]
            z = np.eye(self.artf_num)
            self.A = np.concatenate((A,z), axis = 1)
            self.c = np.concatenate((c, np.ones((self.artf_num))), axis=0)
            self.slack_num = 0

            self.num_var = self.A.shape[1]

        elif (A is None and  not (A_l is None)):

            self.slack_num = A_l.shape[0]
            s = np.eye(self.slack_num)
            self.A = np.concatenate((A_l,s), axis = 1)
            self.b=b_l
            self.artf_num = 0
            self.c = np.concatenate((c, np.zeros((self.slack_num))), axis=0)

            self.num_var = self.A.shape[1]

        elif (not (A is None) and  not (A_l is None)):

            self.slack_num = A_l.shape[0]
            s = np.eye(self.slack_num)
            s_ = np.zeros( (A.shape[0], self.slack_num) )
            self.artf_num = A.shape[0]
            z = np.eye(self.artf_num)
            z_ =  np.zeros( (A_l.shape[0], self.artf_num) )
            A_ = np.concatenate((A, z), axis=1)
            A_ = np.concatenate((A_, s_), axis=1)
            A_l_ = np.concatenate((A_l, z_), axis=1)
            A_l_ = np.concatenate((A_l_, s), axis=1)
            self.A = np.concatenate((A_,A_l_), axis=0)
            self.b = np.concatenate((b,b_l), axis=0)
            self.c = np.concatenate((c, 1000000*np.ones((self.artf_num)),
                                     np.zeros(self.slack_num)), axis=0)

            self.num_var = self.A.shape[1]

    def solve(self):

        x_b, x_n, x_b_val = self.init_base()
        opt = False
        z = 0

        ix=1
        while not opt:

            x_b, x_n, x_b_val, z, opt, status = self.iterate(x_b,x_n)

            ix+=1

        return x_b, x_n, x_b_val, z, status

    def init_base(self):

        x_b = [i for i in range(self.num_var - (self.artf_num+self.slack_num),
                   ...


I am working on the file "SimplexAlgorithm.ipynb".
The first task is:
Show results
Can you write Python code for it?

for i in range(len(x_b)):
  print("Variable: x_{} , value: {} ".format(str(x_b[i]+1 ),x_b_val[i]  ))


I am working on the file "Simulated Network Example.ipynb".
The first task is:
Contents

Part I. Setup & Motivation
Part II. Sparse MLE
Part III. Compare Estimators
Part IV. Model Averaging

If kernel dies, go here

Part I. Setup & Motivation
Create Simulated Network
Can you write Python code for it?

%matplotlib inline
covariance, precision, adjacency = new_graph(15,.15,adj_type=adj_type,random_sign=True,seed=1)
covariance2, precision2, adjacency2 = new_graph(15,.2,adj_type=adj_type,random_sign=True, seed=1)

# Set up the matplotlib figure
f, (ax1, ax2, ax3) = plt.subplots(1,3, figsize=(10, 4));

def compare_population_parameters(covariance,precision,adjacency):
    mask = np.zeros_like(precision, dtype=np.bool)
    mask[np.triu_indices_from(mask)] = True
    mask[np.where(np.eye(np.shape(precision)[0]))] = True

    # Generate a custom diverging colormap
    cmap = sns.diverging_palette(220, 10, as_cmap=True)

    # Draw the heatmap with the mask and correct aspect ratio
    adj_vmax = np.max(np.triu(adjacency,1))
    sns.heatmap(adjacency,mask=mask,cmap=cmap, vmax=adj_vmax,
                square=True, xticklabels=5, yticklabels=5,
                linewidths=.5, cbar_kws={"shrink": .5}, ax=ax1)
    ax1.set_title('Adjacency of Precision Matrix')

    prec_vmax = np.max(np.triu(precision,1))
    sns.heatmap(precision,mask=mask,cmap=cmap, vmax=prec_vmax,
                square=True, xticklabels=5, yticklabels=5,
                linewidths=.5, cbar_kws={"shrink": .5}, ax=ax2)
    ax2.set_title('Precision Matrix')

    cov_vmax = np.max(np.triu(covariance,1))
    sns.heatmap(covariance, mask=mask,cmap=cmap, vmax=cov_vmax,
                square=True, xticklabels=5, yticklabels=5,
                linewidths=.5, cbar_kws={"shrink": .5}, ax=ax3)
    ax3.set_title('Covariance Matrix')

compare_population_parameters(covariance,precision,adjacency)


I am working on the file "Simulated Network Example.ipynb".
The first task is:
High Sample Size
Can you write Python code for it?

%matplotlib inline
f, (ax1, ax2, ax3) = plt.subplots(1,3, figsize=(10, 4));

n_samples = 75*n_features
prng = np.random.RandomState(2)
X = mvn(n_samples,n_features,covariance,random_state=prng)
print 'n = {},p = {}'.format(n_samples,n_features)

compare_init_adaptive(X,n_samples,n_features)


I am working on the file "Simulated Network Example.ipynb".
The first task is:
Initial vs. Adaptive in Moderately Dense Graphs
Can you write Python code for it?

%matplotlib inline
f, (ax1,ax2,ax3) = plt.subplots(1,3,figsize=(10,4));
covariance, precision, adjacency = new_graph(15,.4,adj_type=adj_type,random_sign=True,seed=1)
compare_population_parameters(covariance,precision,adjacency)


I am working on the file "Simulated Network Example.ipynb".
The first task is:
Initial vs. Adaptive, Low Sample Size
Can you write Python code for it?

%matplotlib inline
f, (ax1,ax2,ax3) = plt.subplots(1,3,figsize=(10,4));

n_samples = 15*n_features
prng = np.random.RandomState(2)
X = mvn(n_samples,n_features,covariance,random_state=prng)
print 'n = {},p = {}'.format(n_samples,n_features)
compare_init_adaptive(X,n_samples,n_features)


I am working on the file "Simulated Network Example.ipynb".
The first task is:
Low Sample Size
Can you write Python code for it?

%matplotlib inline
f, (ax1, ax2, ax3) = plt.subplots(1,3, figsize=(10, 4));

n_samples = 20*n_features
prng = np.random.RandomState(2)
X = mvn(n_samples,n_features,covariance,random_state=prng)
print 'n = {},p = {}'.format(n_samples,n_features)

compare_init_adaptive(X,n_samples,n_features)


I am working on the file "Simulated Network Example.ipynb".
The first task is:
Model Selection: Cross-Validation versus EBIC
Can you write Python code for it?

metric='log_likelihood';
covariance, precision, adjacency = new_graph(n_features,.05,adj_type='banded',random_sign=True,seed=1) # BUG:Might fail with other graphs
prng = np.random.RandomState(2)
X = mvn(10*n_features,n_features,covariance,random_state=prng)

print 'QuicGraphLassoCV with:'
print '   metric: {}'.format(metric)
cv_model = QuicGraphLassoCV(
        cv=2,
        n_refinements=6,
        n_jobs=1,
        init_method='cov',
        score_metric=metric)
cv_model.fit(X)
cv_precision_ = cv_model.precision_
print '   len(cv_lams): {}'.format(len(cv_model.cv_lams_))
print '   lam_scale_: {}'.format(cv_model.lam_scale_)
print '   lam_: {}'.format(cv_model.lam_)

# EBIC
gamma = .1 # gamma = 0 equivalent to BIC and gamma=.5 for ultra high dimensions
ebic_model = QuicGraphLassoEBIC(
    lam=1.0,
    init_method='cov',
    gamma = gamma)
ebic_model.fit(X)
ebic_precision_ = ebic_model.precision_
print 'QuicGraphLassoEBIC with:'
print '   len(path lams): {}'.format(len(ebic_model.path_))
print '   lam_scale_: {}'.format(ebic_model.lam_scale_)
print '   lam_: {}'.format(ebic_model.lam_)

%matplotlib inline
f, (ax1,ax2,ax3) = plt.subplots(1,3,figsize=(10,4));

mask = np.zeros_like(precision, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True
mask[np.where(np.eye(np.shape(precision)[0]))] = True
cmap = sns.diverging_palette(220, 10, as_cmap=True)

prec_vmax = np.max(np.triu(np.abs(adjacency),1))
sns.heatmap(np.abs(adjacency),cmap=cmap, vmax=prec_vmax,
            square=True, xticklabels=5, yticklabels=5,
            linewidths=.5, cbar_kws={"shrink": .5},ax=ax1)
ax1.set_title('True Precision, CV')

prec_vmax = np.max(np.triu(np.abs(cv_precision_),1))
sns.heatmap(np.abs(cv_precision_),cmap=cmap, vmax=prec_vmax,
            square=True, xticklabels=5, yticklabels=5,
            linewidths=.5, cbar_kws={"shrink": .5},ax=ax2)
ax2.set_title('Selected Precision, CV')

prec_vmax = np.max(np.triu(np.abs(ebic_precision_),1))
sns.heatmap(np.abs(ebic_precision_),cmap=cmap, vmax=prec_vmax,
            square=True,...


I am working on the file "Simulated Network Example.ipynb".
The first task is:
Part III. Compare Estimators: Initial vs. Adaptive

Initial vs. Adaptive, High Sample Size
Can you write Python code for it?

%matplotlib inline
f, (ax1,ax2,ax3) = plt.subplots(1,3,figsize=(10,4));

covariance, precision, adjacency = new_graph(15,.15,adj_type=adj_type,random_sign=True,seed=1)
n_samples = 75*n_features
prng = np.random.RandomState(2)
X = mvn(n_samples,n_features,covariance,random_state=prng)

print 'n = {},p = {}'.format(n_samples,n_features)

def compare_init_adaptive(X,n_samples,n_features):
    # Initial Estimator
    initial_estimator = QuicGraphLassoCV(init_method='corrcoef')
    initial_estimator.fit(X)
    prec_hat = initial_estimator.precision_

    # Generate a custom diverging colormap
    cmap = sns.diverging_palette(220, 10, as_cmap=True)
    prec_vmax = np.max(np.triu(prec_hat,1))
    sns.heatmap(initial_estimator.precision_,cmap=cmap, vmax=prec_vmax,
                square=True, xticklabels=5, yticklabels=5,
                linewidths=.5, cbar_kws={"shrink": .5},ax=ax1)
    ax1.set_title('Precision Matrix, Initial Estimator')
    # Check Average Power
    err_frob, err_supp, err_fp, err_fn, err_inf =  ae_trial(
        trial_estimator=initial_estimator,
        n_samples=n_samples,
        n_features=n_features,
        cov=covariance,
        adj=adjacency,
        random_state=np.random.RandomState(2),X=X)
    print 'Difference in sparsity: {},{}'.format(
        np.sum(np.not_equal(precision,0)),
        np.sum(np.not_equal(initial_estimator.precision_,0))
    )
    print 'Frob Norm: {} ({}), Support Error: {}, False Pos: {}, False Neg: {}'.format(
        err_frob, err_inf,
        err_supp,
        err_fp,
        err_fn
    )
    # Adaptive Estimator
    twostage = AdaptiveGraphLasso(estimator=initial_estimator,method='inverse')
    twostage.fit(X)
    weighted_estimator = twostage.estimator_

    prec_hat = weighted_estimator.precision_
    prec_vmax = np.max(np.triu(prec_hat,1))
    sns.heatmap(weighted_estimator.precision_,cmap=cmap, vmax=prec_vmax,
                square=True, xticklabels=5, yticklabels=5,
                linewidths=.5, cbar_kws={"shrink": .5},ax=ax2)
   ...


I am working on the file "Simulated_annealing_in_Python.ipynb".
The first task is:
Algorithm
The following pseudocode presents the simulated annealing heuristic.

It starts from a state $s_0$ and continues to either a maximum of $k_{\max}$ steps or until a state with an energy of $e_{\min}$ or less is found.
In the process, the call $\mathrm{neighbour}(s)$ should generate a randomly chosen neighbour of a given state $s$.
The annealing schedule is defined by the call $\mathrm{temperature}(r)$, which should yield the temperature to use, given the fraction $r$ of the time budget that has been expended so far.

Simulated Annealing:

Let $s$ = $s_0$
For $k = 0$ through $k_{\max}$ (exclusive):
$T := \mathrm{temperature}(k ∕ k_{\max})$
Pick a random neighbour, $s_{\mathrm{new}} := \mathrm{neighbour}(s)$
If $P(E(s), E(s_{\mathrm{new}}), T) \geq \mathrm{random}(0, 1)$:
$s := s_{\mathrm{new}}$

Output: the final state $s$

Basic but generic Python code
Let us start with a very generic implementation:
Can you write Python code for it?

def annealing(random_start,
              cost_function,
              random_neighbour,
              acceptance,
              temperature,
              maxsteps=1000,
              debug=True):
    """ Optimize the black-box function 'cost_function' with the simulated annealing algorithm."""
    state = random_start()
    cost = cost_function(state)
    states, costs = [state], [cost]
    for step in range(maxsteps):
        fraction = step / float(maxsteps)
        T = temperature(fraction)
        new_state = random_neighbour(state, fraction)
        new_cost = cost_function(new_state)
        if debug: print("Step #{:>2}/{:>2} : T = {:>4.3g}, state = {:>4.3g}, cost = {:>4.3g}, new_state = {:>4.3g}, new_cost = {:>4.3g} ...".format(step, maxsteps, T, state, cost, new_state, new_cost))
        if acceptance_probability(cost, new_cost, T) > rn.random():
            state, cost = new_state, new_cost
            states.append(state)
            costs.append(cost)
            # print("  ==> Accept it!")
        # else:
        #    print("  ==> Reject it...")
    return state, cost_function(state), states, costs


I am working on the file "Simulated_annealing_in_Python.ipynb".
The first task is:
Basic example
We will use this to find the global minimum of the function $x \mapsto x^2$ on $[-10, 10]$.
Can you write Python code for it?

interval = (-10, 10)

def f(x):
    """ Function to minimize."""
    return x ** 2

def clip(x):
    """ Force x to be in the interval."""
    a, b = interval
    return max(min(x, b), a)

def random_start():
    """ Random point in the interval."""
    a, b = interval
    return a + (b - a) * rn.random_sample()

def cost_function(x):
    """ Cost of x = f(x)."""
    return f(x)

def random_neighbour(x, fraction=1):
    """Move a little bit x, from the left or the right."""
    amplitude = (max(interval) - min(interval)) * fraction / 10
    delta = (-amplitude/2.) + amplitude * rn.random_sample()
    return clip(x + delta)

def acceptance_probability(cost, new_cost, temperature):
    if new_cost < cost:
        # print("    - Acceptance probabilty = 1 as new_cost = {} < cost = {}...".format(new_cost, cost))
        return 1
    else:
        p = np.exp(- (new_cost - cost) / temperature)
        # print("    - Acceptance probabilty = {:.3g}...".format(p))
        return p

def temperature(fraction):
    """ Example of temperature dicreasing as the process goes on."""
    return max(0.01, min(1, 1 - fraction))


I am working on the file "Simulated_annealing_in_Python.ipynb".
The first task is:
Let's try!
Can you write Python code for it?

annealing(random_start, cost_function, random_neighbour, acceptance_probability, temperature, maxsteps=30, debug=True);


I am working on the file "Simulated_annealing_in_Python.ipynb".
The first task is:
Visualizing the steps
Can you write Python code for it?

def see_annealing(states, costs):
    plt.figure()
    plt.suptitle("Evolution of states and costs of the simulated annealing")
    plt.subplot(121)
    plt.plot(states, 'r')
    plt.title("States")
    plt.subplot(122)
    plt.plot(costs, 'b')
    plt.title("Costs")
    plt.show()

see_annealing(states, costs)


I am working on the file "Simulating measurements.ipynb".
The first task is:
Define a quantum state: $$|\psi\rangle = \frac{1}{\sqrt{5}} |H\rangle + \frac{2}{\sqrt{5}} |V\rangle$$
Can you write Python code for it?

psi = 1/sqrt(5)*H + 2/sqrt(5)*V
psi

# The function to generate a mock data set:
def simulateData(state,oper,size=10000):
    """Generate a simulated data set given a state and measurement operator.

    state -> the prepared state
    oper -> the measurement operator

    Example:
    H = Qobj([[1],[0]])
    V = Qobj([[0],[1]])
    psi = 1/sqrt(5)*H + 2/sqrt(5)*V
    Phv = H*H.dag() - V*V.dag()
    data = simulateData(psi,Phv)

    will generate 10000 values in the data array that obey the probability defined in the state.
    """
    A = basis(2,0)
    B = basis(2,1)
    allowed_results = [r.data.data[0] for r in [A.dag()*oper*A, B.dag()*oper*B]]
    probability_amps = [qo.data.data[0] for qo in [A.dag()*state, B.dag()*state]]
    pvals = [abs(pa.conjugate()*pa) for pa in probability_amps]
    data = random.choice(allowed_results,size=size,p=pvals)
    return data

data = simulateData(psi,Phv)

print("Variance: ",data.var())
print("Mean: ",data.mean())

plt.hist(real(data))


I am working on the file "Simulating measurements.ipynb".
The first task is:
Define several standard states, these are photon polarization states:
Can you write Python code for it?


I am working on the file "Simulating_POVMs.ipynb".
The first task is:
As the last step, we iterate the SDP optimization described in Section "Checking criticial visibility" over all vertices to get the best shrinking factor. This takes several hours to complete. Parallel computations do not work from a notebook, but they do when the script is executed in a console. For this reason, here we disable parallel computations.
Can you write Python code for it?

time0 = time.time()
alphas = find_best_shrinking_factor(ext, 2, solver="mosek", parallel=False)
print("\n Found in %d seconds" % (time.time()-time0))


I am working on the file "Simulating_POVMs.ipynb".
The first task is:
Checking criticial visibility
The question whether a qubit or qutrit POVM is simulable by projective measurements boils down to an SDP feasibility problem. Few SDP solvers can handle feasibility problems, so from a computational point of view, it is always easier to phrase the question as an SDP optimization, which would return the critical visibility, below which the amount of depolarizing noise would allow simulability. Recall Eq. (8) from the paper that defines the noisy POVM that we obtain subjecting a POVM $\mathbf{M}$ to a depolarizing channel $\Phi_t$:
$\left[\Phi_t\left(\mathbf{M}\right)\right]_i := t M_i + (1-t)\frac{\mathrm{tr}(M_i)}{d} \mathbb{1}$.
If this visibility $t\in[0,1]$ is one, the POVM $\mathbf{M}$ is simulable.
Qubit example
As an example, we study the tetrahedron measurement (see Appendix B in arXiv:quant-ph/0702021):
Can you write Python code for it?

def dp(v):
    result = np.eye(2, dtype=np.complex128)
    for i in range(3):
        result += v[i]*Pauli[i]
    return result

b = [np.array([ 1,  1,  1])/np.sqrt(3),
     np.array([-1, -1,  1])/np.sqrt(3),
     np.array([-1,  1, -1])/np.sqrt(3),
     np.array([ 1, -1, -1])/np.sqrt(3)]
M = [dp(bj)/4 for bj in b]


I am working on the file "Simulating_POVMs.ipynb".
The first task is:
Decomposition of qutrit three-outcome, trace-one POVMS into projective measurements
We implemented the constructive strategy to find a projective decomposition for trace-one qutrit measurements $\mathbf{M}\in\mathcal{P}_1(3,3)$ we described in Appendix D.
First we define a function to generate a random trace-1 POVM. This is the only step that requires an additional dependency compared to the ones we loaded in the beginning of the notebook. The dependency is QuTiP.
Can you write Python code for it?

from qutip import rand_unitary

def get_random_trace_one_povm(dim=3):
    U = rand_unitary(dim)
    M = [U[:, i]*dag(U[:, i]) for i in range(dim)]
    for _ in range(dim-1):
        U = rand_unitary(dim)
        r = random.random()
        for i in range(dim):
            M[i] = r*M[i] + (1-r)*U[:, i]*dag(U[:, i])
    return M


I am working on the file "Simulating_POVMs.ipynb".
The first task is:
External polytopes approximating $\mathcal{P}_{\mathrm{cov}}(3,9)$
Following the same reasoning applied to approximate $\mathcal{P}(2,4)$ we now approximate the set of qutrit covariant POVMs $\mathcal{P}{\mathrm{cov}}(3,9)$ regarding the discrete Heinsenberg group. This task is relatively simple, since we need only to consider a quasi-positive seed $M$ and derive the effects from it by conjugating by the unitaries $D{ij}$, which rotate the space by symmetric directions.
Can you write Python code for it?


I am working on the file "Simulating_POVMs.ipynb".
The first task is:
Next we check with an SDP whether it is simulable by projective measurements. A four outcome qubit POVM $\mathbf{M} \in\mathcal{P}(2,4)$ is simulable if and only if
$M_{1}=N_{12}^{+}+N_{13}^{+}+N_{14}^{+},$
$M_{2}=N_{12}^{-}+N_{23}^{+}+N_{24}^{+},$
$M_{3}=N_{13}^{-}+N_{23}^{-}+N_{34}^{+},$
$M_{4}=N_{14}^{-}+N_{24}^{-}+N_{34}^{-},$
where Hermitian operators $N_{ij}^{\pm}$ satisfy $N_{ij}^{\pm}\geq0$ and $N_{ij}^{+}+N_{ij}^{-}=p_{ij}\mathbb{1}$, where $i<j$ , $i,j=1,2,3,4$ and $p_{ij}\geq0$ as well as $\sum_{i<j}p_{ij}=1$, that is, the $p_{ij}$ values form a probability vector. This forms an SDP feasibility problem, which we can rephrase as an optimization problem by adding depolarizing noise to the left-hand side of the above equations and maximizing the visibility $t$:
$\max_{t\in[0,1]} t$
such that
$t\,M_{1}+(1-t)\,\mathrm{tr}(M_{1})\frac{\mathbb{1}}{2}=N_{12}^{+}+N_{13}^{+}+N_{14}^{+},$
$t\,M_{2}+(1-t)\,\mathrm{tr}(M_{2})\frac{\mathbb{1}}{2}=N_{12}^{-}+N_{23}^{+}+N_{24}^{+},$
$t\,M_{3}+(1-t)\,\mathrm{tr}(M_{3})\frac{\mathbb{1}}{2}=N_{13}^{-}+N_{23}^{-}+N_{34}^{+},$
$t\,M_{4}+(1-t)\,\mathrm{tr}(M_{4})\frac{\mathbb{1}}{2}=N_{14}^{-}+N_{24}^{-}+N_{34}^{-}$.
If it is, the critical visibility is one, we have a simulable measurement. We solve this SDP with the function get_visibility for the tetrahedron measurement, indicating that it is not simulable:
Can you write Python code for it?


I am working on the file "Simulating_POVMs.ipynb".
The first task is:
Next we look at a projective-simulable POVM:
Can you write Python code for it?

psi = [get_random_qutrit()]
psi.append(complex_cross_product(psi[0], np.array([[0], [0], [1]])))
psi.append(complex_cross_product(psi[0], psi[1]))
phi = [get_random_qutrit()]
phi.append(complex_cross_product(phi[0], np.array([[0], [0], [1]])))
phi.append(complex_cross_product(phi[0], phi[1]))
M = [0.5*np.kron(psi[0], psi[0].conj().T),
     0.5*np.kron(psi[1], psi[1].conj().T),
     0.5*np.kron(psi[2], psi[2].conj().T) + 0.5*np.kron(phi[0], phi[0].conj().T),
     0.5*np.kron(phi[1], phi[1].conj().T),
     0.5*np.kron(phi[2], phi[2].conj().T),
     np.zeros((3, 3), dtype=np.float64),
     np.zeros((3, 3), dtype=np.float64),
     np.zeros((3, 3), dtype=np.float64),
     np.zeros((3, 3), dtype=np.float64)]


I am working on the file "Simulating_POVMs.ipynb".
The first task is:
Next, we would like to constrain the third effect $M_3$, and we start this approximation by defining a polytope approximating the unit sphere in $\mathbb{R}^3$. Similiarly to the previous case, the approximation is defined by the points of tangency to the sphere, provided by the stereographic projection of a set of rational points contained in $[-1, 1]\times[-1, 1]$.
Can you write Python code for it?

m1 = 2
m2 = 1
# crit is the same as above
mm1 = Fraction(1, m1)
mm2 = Fraction(crit, m2)

# v1 discretizes the positive octant of the sphere
v1 = []

# P = (0, 0, -1), x, y \in [0, 1]
for rx in range(1, m1 + 1):
    for ry in range(1, m1 + 1):
        v1.append([Fraction(2*(rx*mm1), (rx*mm1)**2 + (ry*mm1)**2 + 1),
                   Fraction(2*(ry*mm1), (rx*mm1)**2 + (ry*mm1)**2 + 1),
                   1 - Fraction(2, (rx*mm1)**2 + (ry*mm1)**2 + 1)])

# a second round to improve the approximation around the pole
# P = (0, 0, -1), x, y \in [0, crit]
for rx in range(1, m2 + 1):
    for ry in range(1, m2 + 1):
        v1.append([Fraction(2*(rx*mm2), (rx*mm2)**2 + (ry*mm2)**2 + 1),
                   Fraction(2*(ry*mm2), (rx*mm2)**2 + (ry*mm2)**2 + 1),
                   1 - Fraction(2, (rx*mm2)**2 + (ry*mm2)**2 + 1)])

v1 = np.array(v1)

# we now reflect the positive octant to construct the whole sphere
v1a = np.column_stack((-v1[:, 0], v1[:, 1], v1[:, 2]))
v1 = np.row_stack((v1, v1a))
v1b = np.column_stack((v1[:, 0], -v1[:, 1], v1[:, 2]))
v1 = np.row_stack((v1, v1b))
v1c = np.column_stack((v1[:, 0], v1[:, 1], -v1[:, 2]))
v1 = np.row_stack((v1, v1c))

# the following discretizes the quarters of equators where x, y, z > 0,
# corresponding to the case where rx, ry = 0 above, around the origin
yz = []
xz = []
xy = []
for r in range(1, m1+1):
    # P = [0, 0, -1], x = 0, y \in [0, 1]
    yz.append([0,
               Fraction(2*(r*m1), (r*m1)**2 + 1),
               1 - Fraction(2, (r*m1)**2 + 1)])
    # P = [0, 0,-1], y = 0, x \in [0, 1]
    xz.append([Fraction(2*(r*m1), (r*m1)**2 + 1),
               0,
               1 - Fraction(2, (r*m1)**2 + 1)])
    # P = [0, -1, 0], z = 0, x \in [0, 1]
    xy.append([Fraction(2*(r*m1), (r*m1)**2 + 1),
               1 - Fraction(2, (r*m1)**2 + 1),
               0])

yz = np.array(yz)
xz = np.array(xz)
xy = np.array(xy)

yz1 = np.column_stack((yz[:, 0], -yz[:, 1], yz[:, 2]))
yz2 = np.column_stack((yz[:, 0], yz[:, 1], -yz[:, 2]))
yz3 = np.column_stack((yz[:, 0], -yz[:, 1],...


I am working on the file "Simulating_POVMs.ipynb".
The first task is:
The following constraints ensure that the third operator $M_3$ of the measurement is a quasi-effect, with the approximation given by $v$:
Can you write Python code for it?

W2 = np.zeros((v.shape[0], 9), dtype=fractions.Fraction)
for i in range(v.shape[0]):
    W2[i, 5:] = np.array([1, -v[i, 0], -v[i, 1], -v[i, 2]])


I am working on the file "Simulating_POVMs.ipynb".
The first task is:
The next set of constraints ensures the same condition for the last effect, which we express by $\mathbb{1} - M_1 - M_2 - M_3$:
Can you write Python code for it?


I am working on the file "Simulating_POVMs.ipynb".
The first task is:
Then we decompose a random POVM following the cascade of "rank reductions" described in Appendix D, and check the ranks:
Can you write Python code for it?

M = get_random_trace_one_povm()
print("Rank of POVM: ", check_ranks(M))
coefficients, projective_measurements = decomposePovmToProjective(M)


I am working on the file "Simulating_POVMs.ipynb".
The first task is:
We can also look only for the visibility needed to decompose the POVM into general 3-outcome POVMs.
Can you write Python code for it?


I am working on the file "Simulating_POVMs.ipynb".
The first task is:
We enumerate the vertices, which is a time-consuming operation:
Can you write Python code for it?

time0 = time.time()
ext = enumerate_vertices(hull, method="plrs", verbose=1)
print("Vertex enumeration in %d seconds" % (time.time()-time0))


I am working on the file "Simulating_POVMs.ipynb".
The first task is:
We then construct the polytope generated by these inequalities.
Can you write Python code for it?

cov_ext = enumerate_vertices(np.array(hull), method="plrs")


I am working on the file "Simulating_POVMs.ipynb".
The first task is:
We use the the approximation to the set of positive semi-definite operators given by $tr(M\Psi_i)\geq0$, for some finite set of rank-one projectors ${\Psi_i}$. We generate random projectors and rotate them by the unitaries $D_{ij}$ above in order to obtain a more regular distribution in the space of projectors.
Can you write Python code for it?

# Discretization of the set of PSD matrices with 9*N elements
N = 2
disc = []
for _ in range(N):
    psi = np.matrix(qutip.Qobj.full(qutip.rand_ket(3)))
    for j in range(3):
        for k in range(3):
            disc.append(D[j][k]*(psi*dag(psi))*dag(D[j][k]))


I am working on the file "Simulation - CPW-Filter-qubit/.ipynb_checkpoints/SimPSI - Qubit-Cavity-NR Dispersive limit 02-checkpoint.ipynb".
The first task is:
Dispersive Regime
Can you write Python code for it?

U = exp((x * (  -Dagger(a) * sm + a * sp )).expand())

U

H1 = hamiltonian_transformation(U, H, expansion_search=False, N=4).expand()

H1 = qsimplify(H1)

# H1

H2 = drop_terms_containing(H1.expand(), [ x**4,x**5,x**6])

# H2

H3 = H2.subs(x, g/Delta_CPW)

# H3

H4 = drop_c_number_terms(H3)

# H4

H5 = collect(H4, [Dagger(a) * a, sz])

# H5

U = exp(I * omega_r * t * Dagger(a) * a)

H6 = hamiltonian_transformation(U, H5.expand(),independent = True);
# H6

U = exp(I * omega_q * t * Dagger(sm) * sm)

H7 = hamiltonian_transformation(U, H6.expand(),independent=True);
# H7

H8 = drop_terms_containing(H7, [exp(I * omega_r * t), exp(-I * omega_r * t),
                                exp(I * omega_q * t), exp(-I * omega_q * t)])

# H8

H9 = qsimplify(H8)

H9 = collect(H9, [Dagger(a) * a, sz])

# H9

U = exp(-I * omega_r * t * Dagger(a) * a)

H10 = hamiltonian_transformation(U, H9.expand(),independent = True);
# H10

U = exp(-I * omega_q * t * Dagger(sm) * sm)

H11 = hamiltonian_transformation(U, H10.expand(),independent=True);
# H11

# H11  = drop_c_number_terms(H11)
H12 = qsimplify(H11)

H12 = collect(H12, [Dagger(a) * a, sz])

H12 = H12.subs(omega_r, omega_q-Delta_CPW).expand().collect([Dagger(a)*a, Dagger(b)*b, sz, L*g/2/Delta_CPW]).subs(omega_q-Delta_CPW,omega_r)

Eq(Hsym, H12)


I am working on the file "Simulation - CPW-Filter-qubit/.ipynb_checkpoints/SimPSI - Qubit-Cavity-NR Dispersive limit 02-checkpoint.ipynb".
The first task is:
First move to the interaction picture:
Can you write Python code for it?

U = exp(I * omega_r * t * Dagger(a) * a)

U

H2 = hamiltonian_transformation(U, H_total.expand(),independent = True)

H2

U = exp(I * omega_q * t * sp * sm)

U

H3 = hamiltonian_transformation(U, H2.expand(), independent = True)

H3 = H3.subs(sx, sm + sp).expand()

H3 = powsimp(H3)

H3


I am working on the file "Simulation - CPW-Filter-qubit/.ipynb_checkpoints/SimPSI - Qubit-Cavity-NR Dispersive limit 02-checkpoint.ipynb".
The first task is:
Now, in the rotating-wave approximation we can drop the fast oscillating terms containing the factors $ e^{\pm i( (\omega_q + \omega_r)t}$
Can you write Python code for it?

H5 = drop_terms_containing(H4, [exp( I * (omega_q + omega_r) * t),
                                exp(-I * (omega_q + omega_r) * t)])

H5 = drop_c_number_terms(H5.expand())

Eq(Hsym, H5)


I am working on the file "Simulation - CPW-Filter-qubit/.ipynb_checkpoints/SimPSI - Qubit-Cavity-NR Dispersive limit 02-checkpoint.ipynb".
The first task is:
The Jaynes-Cummings model
The Jaynes-Cummings model is one of the most elementary quantum mechanical models light-matter interaction. It describes a single two-level atom that interacts with a single harmonic-oscillator mode of a electromagnetic cavity.
The Hamiltonian for a two-level system in its eigenbasis can be written as
$$
H = \frac{1}{2}\omega_q \sigma_z
$$
and the Hamiltonian of a quantum harmonic oscillator (cavity or nanomechanical resonator, NR) is
$$
H = \hbar\omega_r (a^\dagger a + 1/2)
$$
$$
H = \hbar\omega_{NR} (b^\dagger b + 1/2)
$$
The atom interacts with the electromagnetic field produced by the cavity (NR) mode $a + a^\dagger$ ($b + b^\dagger$) through its dipole moment. The dipole-transition operators is $\sigma_x$ (which cause a transition from the two dipole states of the atom). The combined atom-cavity Hamiltonian can therefore be written in the form
$$
H =
\hbar \omega_r (a^\dagger a + 1/2)
+
\hbar \omega_{NR} (b^\dagger b + 1/2)
+ \frac{1}{2}\hbar\Omega\sigma_z
+
\hbar
\sigma_x \left( g(a + a^\dagger)
+
\lambda(b + b^\dagger)\right)
$$
Although the Jaynes-Cumming Hamiltonian allow us to evolve the given initial state according to the Schrödinger Equation, in an experiment we would like to predicte the response of the coupled cavity-NR-qubit system under the influence of driving fields for the cavity and qubit, and also account for the effects of dissipation and dephasing (not treated here)
The external coherent-state input may be incorporated in the Jaynes-Cummings Hamiltonian by addition of terms involving the amplitude of the driving field $\vec{E_d} \left(\vec{E_s}\right)$ and it's frequency $\omega_d\left(\omega_s\right)$
$$
H_{cavity} =
E_d \left(e^{i\omega_dt}a +e^{-i\omega_dt}a^\dagger\right)
$$
$$
H_{qubit} =
E_s \left(e^{i\omega_st}\sigma_- +e^{-i\omega_st}\sigma_+\right)
$$
$$
H_{NR} =
E_p \left(e^{i\omega_st}b +e^{-i\omega_st}b^\dagger\right)
$$
To obtain the Jaynes-Cumming Hamiltonian
$$
?
H =
\hbar\omega_r (a^\dagger a + 1/2)
%-\frac{1}{2}\Delta\sigma_x
+...
Can you write Python code for it?

# CPW, qubit and NR energies
omega_r, omega_q, omega_nr = symbols("omega_r, omega_q, omega_{NR}")
# Coupling CPW-qubit, NR_qubit
g, L, chi, eps = symbols("g, lambda, chi, epsilon")
# Detuning

# Drives and detunnings

Delta_d, Delta_s, Delta_p  = symbols(" Delta_d, Delta_s, Delta_p ")

A, B, C = symbols("A,B,C") # Electric field amplitude

omega_d, omega_s, omega_p = symbols("omega_d, omega_s, omega_p") # drive frequencies

# Detunning CPW-qubit, NR-qubit

Delta_CPW, Delta_NR = symbols("Delta_{CPW},Delta_{NR}")

# auxilary variables

x, y, t, Hsym = symbols("x, y, t, H ")

Delta_SP, Delta_SD = symbols("Delta_{SP},Delta_{SD}")

# omega_r, omega_q, g, Delta_d, Delta_s, t, x, chi, Hsym = symbols("omega_r, omega_q, g, Delta_d, Delta_s, t, x, chi, H")

# A, B, C = symbols("A,B,C") # Electric field amplitude

# omega_d, omega_s = symbols("omega_d, omega_s") #

# omega_nr, L = symbols("omega_{NR},lambda")

# Delta,Delta_t = symbols("Delta,Delta_t")

# y, omega_t = symbols("y, omega_t")

sx, sy, sz, sm, sp = SigmaX(), SigmaY(), SigmaZ(), SigmaMinus(), SigmaPlus()
a = BosonOp("a")
b = BosonOp("b")

H = omega_r * Dagger(a) * a + omega_q/2 * sz + omega_nr * Dagger(b) * b

H_int = sx * (g  * (a + Dagger(a)))
H_int_2 = sx *( L * (b + Dagger(b)))

H_drive_r = A * (exp(I*omega_d*t)*a + exp(-I*omega_d*t)*Dagger(a))
H_drive_q = B * (exp(I*omega_s*t)*sm + exp(-I*omega_s*t)*sp)
H_drive_NR = C * (exp(I*omega_p*t)*b + exp(-I*omega_p*t)*Dagger(b))
H_total  =  H +   H_drive_r + H_drive_q + H_drive_NR #+ H_int + H_int_2 +
Eq(Hsym,H_total)


I am working on the file "Simulation - CPW-Filter-qubit/.ipynb_checkpoints/SimPSI - Qubit-Cavity-NR Dispersive limit 02-checkpoint.ipynb".
The first task is:
This is the interaction term of in the Jaynes-Cumming model in the interaction picture. If we transform back to the Schrödinger picture we have:
Can you write Python code for it?

U = exp(-I * omega_r * t * Dagger(a) * a)
H6 = hamiltonian_transformation(U, H5.expand(), independent =True)

U = exp(-I * omega_q * t * sp * sm)
H7 = hamiltonian_transformation(U, H6.expand(),independent = True)

H8 = simplify_exp(H7).subs(Delta_CPW, omega_q - omega_r)

H8 = simplify_exp(powsimp(H8)).expand()

H8 = drop_c_number_terms(H8)

H = collect(H8, [g])

Eq(Hsym, H)

H = collect(H,[L*sm])
H = collect(H,[L*sp])
H = collect(H,[L*(Dagger(b)+b), A, B ,C])
H = H.subs(sm+sp,sx)
H


I am working on the file "Simulation - CPW-Filter-qubit/.ipynb_checkpoints/SimPSI - Qubit-Cavity-NR Dispersive limit 02-checkpoint.ipynb".
The first task is:
We introduce the detuning parameter $\Delta = \omega_q - \omega_r$ and substitute into this expression
Can you write Python code for it?

# trick to simplify exponents
def simplify_exp(e):
    if isinstance(e, exp):
        return exp(simplify(e.exp.expand()))

    if isinstance(e, (Add, Mul)):
        return type(e)(*(simplify_exp(arg) for arg in e.args))

    return e

H4 = simplify_exp(H3).subs(-omega_r + omega_q, Delta_CPW)

H4


I am working on the file "Simulation - CPW-Filter-qubit/.ipynb_checkpoints/SimPSI - Qubit-Cavity-NR Dispersive limit 03-checkpoint.ipynb".
The first task is:
Linearized interaction
Can you write Python code for it?

U = exp(I * Dagger(a) * a * omega_d * t)

H1 = hamiltonian_transformation(U, H_total, independent=True)

H1

U = exp(I * Dagger(b) * b * omega_p * t)

H2 = hamiltonian_transformation(U, H1, independent=True)

H2

U = exp(I * Dagger(sm) * sm * omega_s * t)

H3 = hamiltonian_transformation(U, H2, independent=True)

H3

# H4 = simplify_exp(H3).subs(omega_d - omega_s, -Delta_SD)

H4 = H3.collect([A,B,C,Dagger(a)*a,Dagger(b)*b, g, L])

H4

H5 = H4.expand().subs(-sp*sm , -sz/2 ).collect([A,B,C,Dagger(a)*a,Dagger(b)*b, g, L, sz])
H5

H5 = H5.subs(-omega_d + omega_r, -Delta_d).subs(-omega_p + omega_nr, - Delta_p).subs(omega_q/2 - omega_s/2, Delta_s); H5

H_total_01 = H5
Eq(Hsym,H_total_01)

H_total_01 = H_total_01 + H_int + H_int_2;
Eq(Hsym,H_total_01)

U = exp(I * omega_r * t * Dagger(a) * a)

U

H2 = hamiltonian_transformation(U, H_total_01.expand(),independent = True)

H2

U = exp(I * omega_q * t * sp * sm)

U

H3 = hamiltonian_transformation(U, H2.expand(), independent = True)

H3 = H3.subs(sx, sm + sp).expand()

H3 = powsimp(H3)

H3

H4 = simplify_exp(H3).subs(-omega_r + omega_q, Delta_CPW)

H4

H5 = drop_terms_containing(H4, [exp( I * (omega_q + omega_r) * t),
                                exp(-I * (omega_q + omega_r) * t)])

H5 = drop_c_number_terms(H5.expand())

Eq(Hsym, H5)

U = exp(-I * omega_r * t * Dagger(a) * a)
H6 = hamiltonian_transformation(U, H5.expand(), independent =True)

U = exp(-I * omega_q * t * sp * sm)
H7 = hamiltonian_transformation(U, H6.expand(),independent = True)

H8 = simplify_exp(H7).subs(Delta_CPW, omega_q - omega_r)

H8 = simplify_exp(powsimp(H8)).expand()

H8 = drop_c_number_terms(H8)

H = collect(H8, [g])

H = collect(H,[L*sm])
H = collect(H,[L*sp])
H = collect(H,[L*(Dagger(b)+b), A, B ,C])
H = H.subs(sm+sp,sx)
H


I am working on the file "Simulation - CPW-Filter-qubit/.ipynb_checkpoints/SimPSI - Qubit-Cavity-NR Dispersive limit 03-checkpoint.ipynb".
The first task is:
Now, in the rotating-wave approximation we can drop the fast oscillating terms containing the factors $ e^{\pm i( (\omega_q + \omega_r)t}$
Can you write Python code for it?