Skip to content

Commit

Permalink
fix: make rotation work when theta/phi is not in increasing order
Browse files Browse the repository at this point in the history
+ run tests on windows and macos as well as ubuntu
  • Loading branch information
pranabdas committed Feb 9, 2023
1 parent 0a7169b commit 9ee0f58
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy-gh-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:

jobs:
deploy:
runs-on: ubuntu-22.04
runs-on: ubuntu-latest
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}

Expand Down
9 changes: 4 additions & 5 deletions .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ on:

jobs:
tests:
runs-on: ubuntu-22.04
runs-on: ${{ matrix.os }}
strategy:
matrix:
python-version: ["3.10", "3.11"]
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: ["3.9", "3.10", "3.11"]
steps:
- uses: actions/checkout@v3
- name: Install Python 3
Expand All @@ -22,9 +23,7 @@ jobs:
- name: Install dependencies
run: |
python3 -m pip install --upgrade pip
if [ -f requirements.txt ]; then
pip install -r requirements.txt
fi
pip install -r requirements.txt
- name: Run tests
run: |
python3 -m unittest tests.py
24 changes: 23 additions & 1 deletion src/rotate_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
"""
Program: Rotate 2D array perpendicular to the plane
Version: 20210601
Version: 20230208
@author: Pranab Das (GitHub: @pranabdas)
"""

Expand All @@ -11,6 +11,19 @@ def rotate_2d(data, rotation, x, y):
import numpy as np
from scipy import interpolate

# transform x and y in increasing order
is_x_flipped = False
if (x[0] > x[-1]):
is_x_flipped = True
x = np.flip(x)
data = np.flip(data, 0)

is_y_flipped = False
if (y[0] > y[-1]):
is_y_flipped = True
y = np.flip(y)
data = np.flip(data, 1)

data[np.isnan(data)] = 0
rotation = np.deg2rad(rotation)
c, s = np.cos(rotation), np.sin(rotation)
Expand Down Expand Up @@ -64,4 +77,13 @@ def rotate_2d(data, rotation, x, y):
data_r = interp.ev(x_temp, y_temp)
data_r[mask] = np.nan

# transform back into original order
if is_x_flipped:
X = np.flip(X)
data_r = np.flip(data_r, 0)

if is_y_flipped:
Y = np.flip(Y)
data_r = np.flip(data_r, 1)

return data_r, X, Y
24 changes: 23 additions & 1 deletion src/rotate_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
"""
Program: Rotate 3D array, axis of rotation is along first dimension
Version: 20210601
Version: 20230208
@author: Pranab Das (GitHub: @pranabdas)
"""

Expand All @@ -11,6 +11,19 @@ def rotate_3d(data, rotation, x, y):
import numpy as np
from scipy import interpolate

# transform x and y in increasing order
is_x_flipped = False
if (x[0] > x[-1]):
is_x_flipped = True
x = np.flip(x)
data = np.flip(data, 1)

is_y_flipped = False
if (y[0] > y[-1]):
is_y_flipped = True
y = np.flip(y)
data = np.flip(data, 2)

data[np.isnan(data)] = 0
rotation = np.deg2rad(rotation)
c, s = np.cos(rotation), np.sin(rotation)
Expand Down Expand Up @@ -67,5 +80,14 @@ def rotate_3d(data, rotation, x, y):
data_r[mask] = np.nan
data_3r.append(data_r)

# transform back into original order
if is_x_flipped:
X = np.flip(X)
data_3r = np.flip(data_3r, 1)

if is_y_flipped:
Y = np.flip(Y)
data_3r = np.flip(data_3r, 2)

data_3r = np.array(data_3r)
return data_3r, X, Y
40 changes: 38 additions & 2 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,19 +251,55 @@ def test_rotate_2d(self):
self.assertEqual(data_r.shape, (439, 439))
self.assertEqual(len(theta_r), 439)
self.assertEqual(len(phi_r), 439)
# self.assertAlmostEqual(data_r[200, 250], 143.48184917)
self.assertAlmostEqual(data_r[200, 250], 143.48184917, places=3)
self.assertAlmostEqual(theta_r[200], -0.88572823)
self.assertAlmostEqual(phi_r[400], 8.15602716)

def test2_rotate_2d(self):
data_r, theta_r, phi_r = rotate_2d(flip(data_map[150, :, :], 0), 45, theta[::-1], phi)
self.assertEqual(data_r.shape, (439, 439))
self.assertEqual(len(theta_r), 439)
self.assertEqual(len(phi_r), 439)
self.assertAlmostEqual(flip(data_r, 0)[200, 250], 143.48184917, places=3)
self.assertAlmostEqual(theta_r[::-1][200], -0.88572823)
self.assertAlmostEqual(phi_r[400], 8.15602716)

def test3_rotate_2d(self):
data_r, theta_r, phi_r = rotate_2d(flip(data_map[150, :, :], 1), 45, theta, phi[::-1])
self.assertEqual(data_r.shape, (439, 439))
self.assertEqual(len(theta_r), 439)
self.assertEqual(len(phi_r), 439)
self.assertAlmostEqual(flip(data_r, 1)[200, 250], 143.48184917, places=3)
self.assertAlmostEqual(theta_r[200], -0.88572823)
self.assertAlmostEqual(phi_r[::-1][400], 8.15602716)

def test_rotate_3d(self):
data_r, theta_r, phi_r = rotate_3d(data_map, 45, theta, phi)
self.assertEqual(data_r.shape, (365, 439, 439))
self.assertEqual(len(theta_r), 439)
self.assertEqual(len(phi_r), 439)
# self.assertAlmostEqual(data_r[200, 250, 150], 1.80742301)
self.assertAlmostEqual(data_r[200, 250, 150], 1.80742301, places=3)
self.assertAlmostEqual(theta_r[200], -0.88572823)
self.assertAlmostEqual(phi_r[400], 8.15602716)

def test2_rotate_3d(self):
data_r, theta_r, phi_r = rotate_3d(flip(data_map, 1), 45, theta[::-1], phi)
self.assertEqual(data_r.shape, (365, 439, 439))
self.assertEqual(len(theta_r), 439)
self.assertEqual(len(phi_r), 439)
self.assertAlmostEqual(flip(data_r, 1)[200, 250, 150], 1.80742301, places=3)
self.assertAlmostEqual(theta_r[::-1][200], -0.88572823)
self.assertAlmostEqual(phi_r[400], 8.15602716)

def test3_rotate_3d(self):
data_r, theta_r, phi_r = rotate_3d(flip(data_map, 2), 45, theta, phi[::-1])
self.assertEqual(data_r.shape, (365, 439, 439))
self.assertEqual(len(theta_r), 439)
self.assertEqual(len(phi_r), 439)
self.assertAlmostEqual(flip(data_r, 2)[200, 250, 150], 1.80742301, places=3)
self.assertAlmostEqual(theta_r[200], -0.88572823)
self.assertAlmostEqual(phi_r[::-1][400], 8.15602716)


if __name__ == "__main__":
unittest.main()

0 comments on commit 9ee0f58

Please sign in to comment.