Skip to content
Snippets Groups Projects
Commit 11602647 authored by Vedrana Andersen Dahl's avatar Vedrana Andersen Dahl
Browse files

week 02 solutions added

parent 317313cd
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:608c7b2a tags:
# LEADERBOARD
%% Cell type:code id:e5396acf tags:
``` python
import quizzes_helper as qh
summary = qh.combine_weeks('_SUBMISSIONS/', week_list = [1, 2, 3, 4, 5, 6, 7, 8, 10])
qh.print_leaderboard(summary)
```
%% Output
w1 w2 w3 w4 w5 w6 w7 w8 w9
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
NielsPeter 27 ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓
Caroline 26 ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✗ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓
Ingi 26 ✓ ✗ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓
Ida Puggaard 25 ✓ ✓ ✓ ✓ ✓ ✗ ✓ ✓ ✓ ✓ ✓ ✓ ✗ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓
J5gbqp 24 ✓ ✗ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✗ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✗
HannahNielsen 23 ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✗ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✗ ✗ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✗ ✓
AstridVesterbrandt 21 ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✗ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✗ ✗ ✓ ✓ ✓ ✗ ✓ ✗ ✓ ✗ ✓
Olczixo 19 ✓ ✗ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✗ ✓ ✓ ✓ ✓ ✗ ✗ ✗ ✗ ✗ ✗ ✓ ✓
s184009 18 ✓ ✗ ✗ ✓ ✗ ✓ ✓ ✓ ✓ ✗ ✓ ✓ ✓ ✓ ✓ ✓ ✗ ✗ ✓ ✓ ✓ ✓ ✓ ✓ ✗ ✗ ✗
ET_Phone_Home 16 ✓ ✓ ✓ ✗ ✓ ✓ ✓ ✓ ✓ ✗ ✗ ✓ ✓ ✓ ✓ - - - - - - ✓ ✓ ✓ ✓ ✗ ✗
RSM 16 ✓ ✗ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✗ ✓ ✓ ✗ ✗ ✗ - - - ✓ ✗ ✗ ✓ ✓ ✗ ✓ ✓ ✓
JuliusKokholm 15 ✓ ✗ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✗ ✓ ✗ ✓ ✓ ✓ - - - - - - - - -
HelenaJensen 15 ✓ ✓ ✗ ✓ ✗ ✓ ✓ ✗ ✗ ✗ ✓ ✓ ✗ ✓ ✓ ✗ ✓ ✗ ✓ ✓ ✓ ✓ ✓ ✗ - - -
jsimonsen 14 ✓ ✓ ✓ - - - - - - ✓ ✓ ✓ ✓ ✓ ✓ ✗ ✓ ✓ - - - - - - ✓ ✓ ✓
Delfin 14 ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✗ ✓ ✓ ✓ ✗ ✗ ✗ ✓ ✓ ✓ - - - - - - ✗ ✗ ✗
SeverinH 14 ✓ ✓ ✓ ✗ ✗ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✗ ✓ ✗ ✓ ✓ ✓ - - - - - - - - -
askefandersen 13 ✓ ✗ ✓ ✓ ✓ ✓ ✗ ✓ ✗ ✓ ✗ ✓ ✗ ✗ ✗ ✗ ✓ ✓ ✓ ✓ ✓ ✗ ✗ ✗ ✗ ✗ ✗
CassandraD 13 ✓ ✓ ✓ ✓ ✓ ✓ - - - ✗ ✓ ✓ ✗ ✓ ✓ - - - ✓ ✓ ✓ - - - - - -
Nojus 10 ✗ ✓ ✗ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ - - - - - - - - - - - - - - -
Denitsa 10 ✓ ✗ ✗ ✓ ✓ ✗ ✓ ✓ ✗ ✗ ✓ ✓ ✗ ✗ ✓ ✗ ✗ ✗ ✓ ✗ ✗ - - - ✗ ✓ ✗
ViktorIsaksen 8 ✓ ✓ ✓ - - - - - - ✗ ✓ ✗ ✓ ✗ ✗ - - - ✓ ✓ ✓ ✗ ✗ ✗ ✗ ✗ ✗
chuan kuang 8 ✗ ✗ ✗ - - - - - - - - - - - - ✓ ✓ ✓ - - - ✓ ✓ ✓ ✓ ✗ ✓
Anonymous_Duck 8 ✓ ✗ ✓ ✓ ✓ ✓ ✗ ✗ ✗ - - - ✓ ✓ ✓ - - - - - - - - - - - -
ngre98 7 ✗ ✓ ✓ ✓ ✓ ✓ ✓ ✗ ✓ - - - - - - - - - - - - - - - - - -
jkb 6 - - - - - - ✓ ✓ ✗ - - - ✗ ✓ ✗ ✗ ✓ ✗ ✓ ✓ ✗ - - - - - -
RyanWasHere 5 ✓ ✗ ✗ - - - - - - - - - - - - - - - - - - ✓ ✓ ✗ ✓ ✗ ✓
EmilRamovic 5 ✗ ✓ ✗ ✓ ✓ ✗ ✗ ✓ ✓ - - - - - - - - - - - - - - - - - -
Louis Kamp 5 ✓ ✓ ✓ ✓ ✗ ✓ - - - - - - - - - - - - - - - - - - - - -
Rami 3 - - - - - - - - - ✓ ✓ ✓ - - - - - - - - - - - - - - -
mbk 3 ✓ ✓ ✓ - - - - - - - - - - - - - - - - - - - - - - - -
Gruny 3 ✓ ✓ ✓ - - - - - - - - - - - - - - - - - - - - - - - -
MihaiA 3 ✓ ✓ ✓ - - - - - - - - - - - - - - - - - - - - - - - -
Tols 3 ✓ ✓ ✓ - - - - - - - - - - - - - - - - - - - - - - - -
DavidOstenfeldt 2 ✓ ✗ ✓ - - - - - - - - - - - - - - - - - - - - - - - -
Peter Jensen 2 ✓ ✗ ✓ - - - - - - - - - - - - - - - - - - - - - - - -
Morten Christensen 2 ✓ ✗ ✓ - - - - - - - - - - - - - - - - - - - - - - - -
ChristianOle 2 ✓ ✗ ✓ - - - - - - - - - - - - - - - - - - - - - - - -
VeloPlusPlus 2 ✓ ✗ ✓ - - - - - - - - - - - - - - - - - - - - - - - -
Watermelon 2 ✓ ✓ ✗ - - - - - - - - - - - - - - - - - - - - - - - -
HelloFromCanada 2 ✓ ✓ ✗ - - - - - - - - - - - - - - - - - - - - - - - -
Leonardo Rodovero 1 ✓ ✗ ✗ - - - - - - - - - - - - - - - - - - - - - - - -
SophusBarlebo 1 ✓ ✗ ✗ - - - - - - - - - - - - - - - - - - - - - - - -
oliverHagel 1 ✓ ✗ ✗ - - - - - - - - - - - - - - - - - - - - - - - -
Rose 0 ✗ ✗ ✗ - - - - - - - - - - - - - - - - - - - - - - - -
Michalina 0 ✗ ✗ ✗ - - - - - - - - - - - - - - - - - - - - - - - -
%% Cell type:code id:a0d98b67 tags:
``` python
qh.plot_overview(summary)
```
%% Output
%% Cell type:markdown id:ddf7eb78 tags:
# Finding the lucky quiz participant
%% Cell type:code id:65872a3f tags:
``` python
chance = summary[['display name']].copy()
chance[['from', 'to']] = 0
chance['to'] = summary.iloc[:, 2:].notna().sum(axis=1).cumsum()
chance['from'] = chance['to'].shift(1).fillna(0).astype(int)
chance['to'] = chance['to'] - 1
print(chance.to_string(index=False, header=True))
```
%% Output
display name from to
NielsPeter 0 26
Caroline 27 53
Ingi 54 80
Ida Puggaard 81 107
J5gbqp 108 134
HannahNielsen 135 161
AstridVesterbrandt 162 188
Olczixo 189 215
s184009 216 242
ET_Phone_Home 243 263
RSM 264 287
JuliusKokholm 288 305
HelenaJensen 306 329
jsimonsen 330 344
Delfin 345 365
SeverinH 366 383
askefandersen 384 410
CassandraD 411 425
Nojus 426 437
Denitsa 438 461
ViktorIsaksen 462 479
chuan kuang 480 491
Anonymous_Duck 492 503
ngre98 504 512
jkb 513 524
RyanWasHere 525 533
EmilRamovic 534 542
Louis Kamp 543 548
Rami 549 551
mbk 552 554
Gruny 555 557
MihaiA 558 560
Tols 561 563
DavidOstenfeldt 564 566
Peter Jensen 567 569
Morten Christensen 570 572
ChristianOle 573 575
VeloPlusPlus 576 578
Watermelon 579 581
HelloFromCanada 582 584
Leonardo Rodovero 585 587
SophusBarlebo 588 590
oliverHagel 591 593
Rose 594 596
Michalina 597 599
%% Cell type:code id:4c14c648 tags:
``` python
import random
import time
random.seed(time.time())
i = random.randint(0, chance['to'][-1])
print(f'Random number is {i}')
winner = (chance['from']<= i) & (chance['to']>= i)
print(f'Lucky quiz participant is {chance[winner]["display name"].values[0]}')
```
%% Cell type:code id:feb044aa tags:
``` python
```
This diff is collapsed.
This diff is collapsed.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Anders Bjorholm Dahl
abda@dtu.dk
"""
import numpy as np
import matplotlib.pyplot as plt
import skimage.io
import skimage.feature
import cv2
#%% Computing Gaussian and its second order derivative
data_path = '../../../../Data/week2/' # replace with your own data path
im = skimage.io.imread(data_path + 'test_blob_uniform.png').astype(np.float)
fig, ax = plt.subplots(1,1,figsize=(10,10),sharex=True,sharey=True)
ax.imshow(im,cmap='gray')
def getGaussDerivative(t):
'''
Computes kernels of Gaussian and its derivatives.
Parameters
----------
t : float
Vairance - t.
Returns
-------
g : numpy array
Gaussian.
dg : numpy array
First order derivative of Gaussian.
ddg : numpy array
Second order derivative of Gaussian
dddg : numpy array
Third order derivative of Gaussian.
'''
kSize = 5
s = np.sqrt(t)
x = np.arange(int(-np.ceil(s*kSize)), int(np.ceil(s*kSize))+1)
x = np.reshape(x,(-1,1))
g = np.exp(-x**2/(2*t))
g = g/np.sum(g)
dg = -x/t*g
ddg = -g/t - x/t*dg
dddg = -2*dg/t - x/t*ddg
return g, dg, ddg, dddg
g, dg, ddg, dddg = getGaussDerivative(3)
fig, ax = plt.subplots(1,1,figsize=(10,10),sharex=True,sharey=True)
ax.plot(g)
ax.plot(dg)
ax.plot(ddg)
ax.plot(dddg)
#%% Convolve an image
t = 325
g, dg, ddg, dddg = getGaussDerivative(t)
Lg = cv2.filter2D(cv2.filter2D(im, -1, g), -1, g.T)
fig, ax = plt.subplots(1,1,figsize=(10,10),sharex=True,sharey=True)
ax.imshow(Lg,cmap='gray')
#%% Detecting blobs on one scale
im = skimage.io.imread(data_path + 'test_blob_uniform.png').astype(np.float)
Lxx = cv2.filter2D(cv2.filter2D(im, -1, g), -1, ddg.T)
Lyy = cv2.filter2D(cv2.filter2D(im, -1, ddg), -1, g.T)
L_blob = t*(Lxx + Lyy)
# how blob response
fig, ax = plt.subplots(1,1,figsize=(10,10),sharex=True,sharey=True)
pos = ax.imshow(L_blob, cmap='gray')
fig.colorbar(pos)
#%% Find regional maximum in Laplacian
magnitudeThres = 50
coord_pos = skimage.feature.peak_local_max(L_blob, threshold_abs=magnitudeThres)
coord_neg = skimage.feature.peak_local_max(-L_blob, threshold_abs=magnitudeThres)
coord = np.r_[coord_pos, coord_neg]
# Show circles
theta = np.arange(0, 2*np.pi, step=np.pi/100)
theta = np.append(theta, 0)
circ = np.array((np.cos(theta),np.sin(theta)))
n = coord.shape[0]
m = circ.shape[1]
fig, ax = plt.subplots(1,1,figsize=(10,10),sharex=True,sharey=True)
ax.imshow(im, cmap='gray')
plt.plot(coord[:,1], coord[:,0], '.r')
circ_y = np.sqrt(2*t)*np.reshape(circ[0,:],(1,-1)).T*np.ones((1,n)) + np.ones((m,1))*np.reshape(coord[:,0],(-1,1)).T
circ_x = np.sqrt(2*t)*np.reshape(circ[1,:],(1,-1)).T*np.ones((1,n)) + np.ones((m,1))*np.reshape(coord[:,1],(-1,1)).T
plt.plot(circ_x, circ_y, 'r')
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Detecting blobs on multiple scales
im = skimage.io.imread(data_path + 'test_blob_varying.png').astype(np.float)
t = 15
g, dg, ddg, dddg = getGaussDerivative(t)
r,c = im.shape
n = 100
L_blob_vol = np.zeros((r,c,n))
tStep = np.zeros(n)
Lg = im
for i in range(0,n):
tStep[i] = t*i
L_blob_vol[:,:,i] = t*i*(cv2.filter2D(cv2.filter2D(Lg, -1, g), -1, ddg.T) +
cv2.filter2D(cv2.filter2D(Lg, -1, ddg), -1, g.T))
Lg = cv2.filter2D(cv2.filter2D(Lg, -1, g), -1, g.T)
#%% find maxima in scale-space
thres = 40.0
coord_pos = skimage.feature.peak_local_max(L_blob_vol, threshold_abs = thres)
coord_neg = skimage.feature.peak_local_max(-L_blob_vol, threshold_abs = thres)
coord = np.r_[coord_pos,coord_neg]
# Show circles
theta = np.arange(0, 2*np.pi, step=np.pi/100)
theta = np.append(theta, 0)
circ = np.array((np.cos(theta),np.sin(theta)))
n = coord.shape[0]
m = circ.shape[1]
fig, ax = plt.subplots(1,1,figsize=(10,10),sharex=True,sharey=True)
ax.imshow(im, cmap='gray')
plt.plot(coord[:,1], coord[:,0], '.r')
scale = tStep[coord[:,2]]
circ_y = np.sqrt(2*scale)*np.reshape(circ[0,:],(1,-1)).T*np.ones((1,n)) + np.ones((m,1))*np.reshape(coord[:,0],(-1,1)).T
circ_x = np.sqrt(2*scale)*np.reshape(circ[1,:],(1,-1)).T*np.ones((1,n)) + np.ones((m,1))*np.reshape(coord[:,1],(-1,1)).T
plt.plot(circ_x, circ_y, 'r')
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Detecting blobs in real data (scale space)
# diameter interval and steps
d = np.arange(10, 24.5, step = 0.4)
tStep = np.sqrt(0.5)*((d/2)**2) # convert to scale
# read image and take out a small part
im = skimage.io.imread(data_path + 'SEM.png').astype(np.float)
im = im[200:500,200:500]
fig, ax = plt.subplots(1,1,figsize=(10,10),sharex=True,sharey=True)
ax.imshow(im, cmap='gray')
#%% Compute scale space
r,c = im.shape
n = d.shape[0]
L_blob_vol = np.zeros((r,c,n))
for i in range(0,n):
g, dg, ddg, dddg = getGaussDerivative(tStep[i])
L_blob_vol[:,:,i] = tStep[i]*(cv2.filter2D(cv2.filter2D(im,-1,g),-1,ddg.T) +
cv2.filter2D(cv2.filter2D(im,-1,ddg),-1,g.T))
#%% Find maxima in scale space
thres = 30
coord = skimage.feature.peak_local_max(-L_blob_vol, threshold_abs = thres)
# Show circles
def getCircles(coord, scale):
'''
Comptue circle coordinages
Parameters
----------
coord : numpy array
2D array of coordinates.
scale : numpy array
scale of individual blob (t).
Returns
-------
circ_x : numpy array
x coordinates of circle. Each column is one circle.
circ_y : numpy array
y coordinates of circle. Each column is one circle.
'''
theta = np.arange(0, 2*np.pi, step=np.pi/100)
theta = np.append(theta, 0)
circ = np.array((np.cos(theta),np.sin(theta)))
n = coord.shape[0]
m = circ.shape[1]
circ_y = np.sqrt(2*scale)*circ[[0],:].T*np.ones((1,n)) + np.ones((m,1))*coord[:,[0]].T
circ_x = np.sqrt(2*scale)*circ[[1],:].T*np.ones((1,n)) + np.ones((m,1))*coord[:,[1]].T
return circ_x, circ_y
scale = tStep[coord[:,2]]
circ_x, circ_y = getCircles(coord[:,0:2], scale)
fig, ax = plt.subplots(1,1,figsize=(10,10),sharex=True,sharey=True)
ax.imshow(im, cmap='gray')
plt.plot(coord[:,1], coord[:,0], '.r')
plt.plot(circ_x, circ_y, 'r')
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Localize blobs - Example high resolution lab X-ray CT - find the coordinates
# using Gaussian smoothing and use the scale space to find the scale
im = skimage.io.imread(data_path + 'CT_lab_high_res.png').astype(np.float)/255
fig, ax = plt.subplots(1,1,figsize=(10,10),sharex=True,sharey=True)
ax.imshow(im, cmap='gray')
# %% Set parameters
def detectFibers(im, diameterLimit, stepSize, tCenter, thresMagnitude):
'''
Detects fibers in images by finding maxima of Gaussian smoothed image
Parameters
----------
im : numpy array
Image.
diameterLimit : numpy array
2 x 1 vector of limits of diameters of the fibers (in pixels).
stepSize : float
step size in pixels.
tCenter : float
Scale of the Gaussian for center detection.
thresMagnitude : float
Threshold on blob magnitude.
Returns
-------
coord : numpy array
n x 2 array of coordinates with row and column coordinates in each column.
scale : numpy array
n x 1 array of scales t (variance of the Gaussian).
'''
radiusLimit = diameterLimit/2
radiusSteps = np.arange(radiusLimit[0], radiusLimit[1]+0.1, stepSize)
tStep = radiusSteps**2/np.sqrt(2)
r,c = im.shape
n = tStep.shape[0]
L_blob_vol = np.zeros((r,c,n))
for i in range(0,n):
g, dg, ddg, dddg = getGaussDerivative(tStep[i])
L_blob_vol[:,:,i] = tStep[i]*(cv2.filter2D(cv2.filter2D(im,-1,g),-1,ddg.T) +
cv2.filter2D(cv2.filter2D(im,-1,ddg),-1,g.T))
# Detect fibre centers
g, dg, ddg, dddg = getGaussDerivative(tCenter)
Lg = cv2.filter2D(cv2.filter2D(im, -1, g), -1, g.T)
coord = skimage.feature.peak_local_max(Lg, threshold_abs = thresMagnitude)
# Find coordinates and size (scale) of fibres
magnitudeIm = np.min(L_blob_vol, axis = 2)
scaleIm = np.argmin(L_blob_vol, axis = 2)
scales = scaleIm[coord[:,0], coord[:,1]]
magnitudes = -magnitudeIm[coord[:,0], coord[:,1]]
idx = np.where(magnitudes > thresMagnitude)
coord = coord[idx[0],:]
scale = tStep[scales[idx[0]]]
return coord, scale
#%% Set parameters
# Radius limit
diameterLimit = np.array([10,25])
stepSize = 0.3
# Parameter for Gaussian to detect center point
tCenter = 20
# Parameter for finding maxima over Laplacian in scale-space
thresMagnitude = 8
# Detect fibres
coord, scale = detectFibers(im, diameterLimit, stepSize, tCenter, thresMagnitude)
# Plot detected fibres
fig, ax = plt.subplots(1,1,figsize=(10,10),sharex=True,sharey=True)
ax.imshow(im, cmap='gray')
ax.plot(coord[:,1], coord[:,0], 'r.')
circ_x, circ_y = getCircles(coord, scale)
plt.plot(circ_x, circ_y, 'r')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment