Skip to content

Commit

Permalink
extend boundary while calculating convolution for laplacian and 2d-cu…
Browse files Browse the repository at this point in the history
…rvature
  • Loading branch information
pranabdas committed Dec 3, 2022
1 parent 2fa988a commit 8500160
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 75 deletions.
6 changes: 1 addition & 5 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
"image": "mcr.microsoft.com/vscode/devcontainers/base:ubuntu-22.04",
// https://github.com/microsoft/vscode-dev-containers/tree/main/containers
"features": {
"ghcr.io/devcontainers/features/python:1": {
"version": "3.11",
"installJupyterlab": true
},
"node": {
"version": "18"
}
Expand Down Expand Up @@ -39,6 +35,6 @@
"ms-python.vscode-pylance",
"ms-python.python"
],
"postCreateCommand": "if [ -f requirements.txt ]; then pip install -r requirements.txt; fi",
"postCreateCommand": "bash ./.devcontainer/postCreateCommands.sh",
"waitFor": "postCreateCommand"
}
9 changes: 9 additions & 0 deletions .devcontainer/postCreateCommands.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
sudo apt update
sudo apt install -y --no-install-recommends \
fonts-open-sans \
python3 \
python3-pip
if [ -f requirements.txt ]; then
pip install -r requirements.txt
fi
8 changes: 4 additions & 4 deletions docs/enhancements.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ Since the $x$ and $y$ scales represent different quantities (units), we also
have a weight factor $w$.

```python showLineNumbers
# diff2, x, y = arp.laplacian(data, x, y, bw=5, w='default')
diff2, x, y = arp.laplacian(data, energy, angle)
# diff2 = arp.laplacian(data, x, y, bw=5, w='default')
diff2 = arp.laplacian(data, energy, angle)

plt.imshow(diff2, vmax=0, cmap='terrain_r')
plt.axis('off')
Expand Down Expand Up @@ -84,8 +84,8 @@ Scientific Instruments **82**, 043712 (2011)](https://doi.org/10.1063/1.3585113)


```python showLineNumbers
# cv2d, x, y = arp.cv2d(data, x, y, bw=5, c1=0.001, c2=0.001, w='default')
cv2d, x, y = arp.cv2d(data, energy, angle)
# cv2d = arp.cv2d(data, x, y, bw=5, c1=0.001, c2=0.001, w='default')
cv2d = arp.cv2d(data, energy, angle)

plt.imshow(cv2d, vmax=0, cmap='terrain_r')
plt.axis('off')
Expand Down
15 changes: 5 additions & 10 deletions docs/glossary.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Returns cropped 2D array and corresponding axes scaling vectors defined by
## cv2d

```python
cv2d, x, y = arp.cv2d(data, x, y, bw=5, c1=0.001, c2=0.001, w='default')
cv2d = arp.cv2d(data, x, y, bw=5, c1=0.001, c2=0.001, w='default')
```

Calculates two-dimensional curvature. The details about the curvature method can
Expand All @@ -49,10 +49,8 @@ features in image plots*, [Review of Scientific Instruments **82**, 043712
- `w` : scaling weight (optional).

**Outputs:**
- `cv2d` : 2D array of curvature. Output array has different shape than the
input array as the edges are removed due to smoothing artifacts.
- `x` : cropped input `x` array to match `cv2d` dimension.
- `y` : cropped input `y` array to match `cv2d` dimension.
- `cv2d` : 2D array of curvature. Output array has the same shape as the input
array.


## export_itx
Expand Down Expand Up @@ -169,7 +167,7 @@ the slit, and perpendicular to the slit directions, respectively.
## laplacian

```python
diff2, x, y = arp.laplacian(data, x, y, bw=5, w='default')
diff2 = arp.laplacian(data, x, y, bw=5, w='default')
```

Laplacian (second order partial derivatives with respect to both energy and
Expand All @@ -185,10 +183,7 @@ angle) of the spectra.

**Outputs:**
- `diff2` : 2D array of spectra after taking second order partial derivative
w.r.t. both energy and angle. This array has different shape than the input
array as the edges are removed due to smoothing artifacts.
- `x` : cropped input `x` array to match `diff2` dimension.
- `y` : cropped input `y` array to match `diff2` dimension.
w.r.t. both energy and angle. This array has the same shape as input array.


## line_profile
Expand Down
57 changes: 34 additions & 23 deletions notebooks/hands-on.ipynb

Large diffs are not rendered by default.

55 changes: 39 additions & 16 deletions notebooks/laplacian.ipynb

Large diffs are not rendered by default.

11 changes: 3 additions & 8 deletions src/cv2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,12 @@ def cv2d(data, x, y, bw=5, c1=0.001, c2=0.001, w='default'):
import numpy as np
from astropy.convolution import convolve, Box2DKernel
# https://docs.astropy.org/en/latest/api/astropy.convolution.Box2DKernel.html
# https://docs.astropy.org/en/latest/api/astropy.convolution.convolve.html#astropy.convolution.convolve

if (w=='default'):
w = data.shape[0]/data.shape[1]

data_smth = convolve(data, Box2DKernel(bw))

# crop the edges
bo = int(bw/2 + 1)
data_smth = data_smth[bo:-bo, bo:-bo]
x = x[bo:-bo]
y = y[bo:-bo]
data_smth = convolve(data, Box2DKernel(bw), boundary='extend')

dE = np.gradient(data_smth, axis=0)
dT = np.gradient(data_smth, axis=1)*w
Expand All @@ -39,4 +34,4 @@ def cv2d(data, x, y, bw=5, c1=0.001, c2=0.001, w='default'):
# jj <= bo+1 or jj >= data.shape[1]-bo-1):
# diff2[ii][jj] = np.nan

return cv2d, x, y
return cv2d
12 changes: 3 additions & 9 deletions src/laplacian.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,11 @@ def laplacian(data, x, y, bw=5, w='default'):
import numpy as np
from astropy.convolution import convolve, Box2DKernel
# https://docs.astropy.org/en/latest/api/astropy.convolution.Box2DKernel.html

# https://docs.astropy.org/en/latest/api/astropy.convolution.convolve.html#astropy.convolution.convolve
if (w=='default'):
w = data.shape[0]/data.shape[1]

data_smth = convolve(data, Box2DKernel(bw))

# crop the edges
bo = int(bw/2 + 1)
data_smth = data_smth[bo:-bo, bo:-bo]
x = x[bo:-bo]
y = y[bo:-bo]
data_smth = convolve(data, Box2DKernel(bw), boundary='extend')

# Laplacian
diff2 = np.gradient(np.gradient(data_smth, axis=0), axis=0) + \
Expand All @@ -32,4 +26,4 @@ def laplacian(data, x, y, bw=5, w='default'):
# jj <= bo+1 or jj >= data.shape[1]-bo-1):
# diff2[ii][jj] = np.nan

return diff2, x, y
return diff2
Binary file modified static/img/cv2d.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified static/img/cv2d.webp
100644 → 100755
Binary file not shown.
Binary file modified static/img/laplacian.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified static/img/laplacian.webp
100644 → 100755
Binary file not shown.

0 comments on commit 8500160

Please sign in to comment.