From 32a903072f98e03ae663da8d8455e507af169c12 Mon Sep 17 00:00:00 2001 From: Kevin Lewis Date: Thu, 27 Jun 2024 11:57:12 -0400 Subject: [PATCH 01/21] added coverage yaml --- .github/workflows/coverage.yml | 40 ++++++++++++++++++++++++++++++++++ tests/requirements-dev.txt | 1 + 2 files changed, 41 insertions(+) create mode 100644 .github/workflows/coverage.yml diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml new file mode 100644 index 000000000..7bc55d436 --- /dev/null +++ b/.github/workflows/coverage.yml @@ -0,0 +1,40 @@ +name: Workflow for Codecov +on: [push, pull_request] +jobs: + run: + runs-on: ubuntu-latest + steps: + - name: Checkout HEXRD + uses: actions/checkout@v4 + with: + path: hexrd + - name: Set up Python 3.11 + uses: actions/setup-python@v4 + with: + python-version: '3.11' + + + - name: Checkout examples + uses: actions/checkout@v4 + with: + repository: HEXRD/examples + path: examples + + - name: Install HEXRD + run: | + pip install . + working-directory: hexrd + + - name: Install requirements-dev.txt + run: | + pip install -r tests/requirements-dev.txt + working-directory: hexrd + + - name: Run tests + env: + HEXRD_EXAMPLE_REPO_PATH: ${{ github.workspace }}/examples + run: | + pytest tests --cov hexrd + working-directory: hexrd + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v3 diff --git a/tests/requirements-dev.txt b/tests/requirements-dev.txt index be86990b3..e079b1cd2 100644 --- a/tests/requirements-dev.txt +++ b/tests/requirements-dev.txt @@ -1,2 +1,3 @@ pytest coloredlogs +pytest-cov \ No newline at end of file From 11cc3dcd6145450723d38f623352a6ea87e1c2ed Mon Sep 17 00:00:00 2001 From: Kevin Lewis Date: Thu, 27 Jun 2024 12:03:57 -0400 Subject: [PATCH 02/21] Rename yaml to not confuse codecov --- .github/workflows/{coverage.yml => codecov.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/workflows/{coverage.yml => codecov.yml} (100%) diff --git a/.github/workflows/coverage.yml b/.github/workflows/codecov.yml similarity index 100% rename from .github/workflows/coverage.yml rename to .github/workflows/codecov.yml From a183b4b3bfbfdb8561d163a70135faaee6dc8f63 Mon Sep 17 00:00:00 2001 From: Kevin Lewis Date: Thu, 27 Jun 2024 12:11:05 -0400 Subject: [PATCH 03/21] workflow testing --- .github/workflows/codecov.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index 7bc55d436..fe00874c3 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -38,3 +38,6 @@ jobs: working-directory: hexrd - name: Upload coverage to Codecov uses: codecov/codecov-action@v3 + with: + working-directory: hexrd + From 4ea4522f7505ff7235e4143b245ae28b0eb210c1 Mon Sep 17 00:00:00 2001 From: Kevin Lewis Date: Thu, 27 Jun 2024 12:29:49 -0400 Subject: [PATCH 04/21] Temporary gitignore change, may fix --- .github/workflows/codecov.yml | 2 +- .gitignore | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index fe00874c3..af6268198 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -37,7 +37,7 @@ jobs: pytest tests --cov hexrd working-directory: hexrd - name: Upload coverage to Codecov - uses: codecov/codecov-action@v3 + uses: codecov/codecov-action@v4 with: working-directory: hexrd diff --git a/.gitignore b/.gitignore index 90147fb75..30a2e0bb0 100644 --- a/.gitignore +++ b/.gitignore @@ -41,15 +41,15 @@ pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports -htmlcov/ -.tox/ -.nox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*.cover +# htmlcov/ +# .tox/ +# .nox/ +# .coverage +# .coverage.* +# .cache +# nosetests.xml +# coverage.xml +# *.cover .hypothesis/ .pytest_cache/ From d1015c328c2f763657a370f8a590fe7a10881c22 Mon Sep 17 00:00:00 2001 From: Kevin Lewis Date: Thu, 27 Jun 2024 12:32:55 -0400 Subject: [PATCH 05/21] Add github token --- .github/workflows/codecov.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index af6268198..0e0adad5b 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -37,7 +37,8 @@ jobs: pytest tests --cov hexrd working-directory: hexrd - name: Upload coverage to Codecov - uses: codecov/codecov-action@v4 + uses: codecov/codecov-action@v4.0.1 with: working-directory: hexrd + token: ${{ secrets.CODECOV_TOKEN }} From 746e16a445ace67e066553df2ee7846a91deb675 Mon Sep 17 00:00:00 2001 From: Kevin Lewis Date: Thu, 27 Jun 2024 12:38:49 -0400 Subject: [PATCH 06/21] test --- .github/workflows/codecov.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index 0e0adad5b..33d30351a 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -39,6 +39,5 @@ jobs: - name: Upload coverage to Codecov uses: codecov/codecov-action@v4.0.1 with: - working-directory: hexrd token: ${{ secrets.CODECOV_TOKEN }} From 1ba45c797be1627d801fa29150e1ddfae9907b6c Mon Sep 17 00:00:00 2001 From: Kevin Lewis Date: Thu, 27 Jun 2024 13:04:23 -0400 Subject: [PATCH 07/21] safe directory codecov --- .github/workflows/codecov.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index 33d30351a..623a09a23 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -8,6 +8,9 @@ jobs: uses: actions/checkout@v4 with: path: hexrd + - name: set safe directory + run: | + git config --global --add safe.directory "${{ github.workspace }}" - name: Set up Python 3.11 uses: actions/setup-python@v4 with: From 53f7576dc6b4f43ea91c268fdf213a6915ac73b0 Mon Sep 17 00:00:00 2001 From: Kevin Lewis Date: Thu, 27 Jun 2024 13:06:55 -0400 Subject: [PATCH 08/21] Change pip install so code coverage works --- .github/workflows/codecov.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index 623a09a23..14fef0039 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -25,7 +25,7 @@ jobs: - name: Install HEXRD run: | - pip install . + pip install -e . working-directory: hexrd - name: Install requirements-dev.txt From cb258a0a42367a4dd7692aa70d61b0a4e15df17d Mon Sep 17 00:00:00 2001 From: Kevin Lewis Date: Thu, 27 Jun 2024 13:14:09 -0400 Subject: [PATCH 09/21] Check if v3 works --- .github/workflows/codecov.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index 14fef0039..86bf45a6c 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -40,7 +40,7 @@ jobs: pytest tests --cov hexrd working-directory: hexrd - name: Upload coverage to Codecov - uses: codecov/codecov-action@v4.0.1 + uses: codecov/codecov-action@v3 with: token: ${{ secrets.CODECOV_TOKEN }} From fc76d9e6ab7de2cedf11d85a7e6a4273ef175db4 Mon Sep 17 00:00:00 2001 From: Kevin Lewis Date: Thu, 27 Jun 2024 13:14:24 -0400 Subject: [PATCH 10/21] check if v3 works --- .github/workflows/codecov.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index 86bf45a6c..520084015 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -8,9 +8,6 @@ jobs: uses: actions/checkout@v4 with: path: hexrd - - name: set safe directory - run: | - git config --global --add safe.directory "${{ github.workspace }}" - name: Set up Python 3.11 uses: actions/setup-python@v4 with: From 310b8c18a4d495bbee2db7f8822ef27b3041a3cd Mon Sep 17 00:00:00 2001 From: Kevin Lewis Date: Thu, 27 Jun 2024 13:24:45 -0400 Subject: [PATCH 11/21] Locate file --- .github/workflows/codecov.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index 520084015..e86a4fc25 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -36,6 +36,11 @@ jobs: run: | pytest tests --cov hexrd working-directory: hexrd + - name: See filesystem + run: | + ls -l + ls -l hexrd + ls -l hexrd/tests - name: Upload coverage to Codecov uses: codecov/codecov-action@v3 with: From 0c65a14cee49f83d3422d8217ccf90ee1bb2a80f Mon Sep 17 00:00:00 2001 From: Kevin Lewis Date: Thu, 27 Jun 2024 13:25:01 -0400 Subject: [PATCH 12/21] locate file --- .github/workflows/codecov.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index e86a4fc25..2004c95e3 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -38,9 +38,9 @@ jobs: working-directory: hexrd - name: See filesystem run: | - ls -l - ls -l hexrd - ls -l hexrd/tests + ls -la + ls -la hexrd + ls -la hexrd/tests - name: Upload coverage to Codecov uses: codecov/codecov-action@v3 with: From dcdc2d286bf3e04c67f83313561e72a18cc510a6 Mon Sep 17 00:00:00 2001 From: Kevin Lewis Date: Thu, 27 Jun 2024 13:29:52 -0400 Subject: [PATCH 13/21] specify file --- .github/workflows/codecov.yml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index 2004c95e3..8dc7b8871 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -36,13 +36,9 @@ jobs: run: | pytest tests --cov hexrd working-directory: hexrd - - name: See filesystem - run: | - ls -la - ls -la hexrd - ls -la hexrd/tests - name: Upload coverage to Codecov uses: codecov/codecov-action@v3 with: token: ${{ secrets.CODECOV_TOKEN }} + file: hexrd/.coverage From 25ef6c31ecd7162c21e1029666a18cad9155e7e4 Mon Sep 17 00:00:00 2001 From: Kevin Lewis Date: Thu, 27 Jun 2024 13:40:26 -0400 Subject: [PATCH 14/21] change working directory --- .github/workflows/codecov.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index 8dc7b8871..19e3353d2 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -40,5 +40,6 @@ jobs: uses: codecov/codecov-action@v3 with: token: ${{ secrets.CODECOV_TOKEN }} - file: hexrd/.coverage + file: .coverage + working-directory: hexrd From 37e6d26e7cdd3d53ef8d1687aa849da6c7bfa161 Mon Sep 17 00:00:00 2001 From: Kevin Lewis Date: Thu, 27 Jun 2024 13:50:40 -0400 Subject: [PATCH 15/21] use xml --- .github/workflows/codecov.yml | 4 ++-- .gitignore | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index 19e3353d2..b02b02b20 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -34,12 +34,12 @@ jobs: env: HEXRD_EXAMPLE_REPO_PATH: ${{ github.workspace }}/examples run: | - pytest tests --cov hexrd + pytest --cov hexrd --cov-report xml:coverage.xml tests working-directory: hexrd - name: Upload coverage to Codecov uses: codecov/codecov-action@v3 with: token: ${{ secrets.CODECOV_TOKEN }} - file: .coverage + file: coverage.xml working-directory: hexrd diff --git a/.gitignore b/.gitignore index 30a2e0bb0..90147fb75 100644 --- a/.gitignore +++ b/.gitignore @@ -41,15 +41,15 @@ pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports -# htmlcov/ -# .tox/ -# .nox/ -# .coverage -# .coverage.* -# .cache -# nosetests.xml -# coverage.xml -# *.cover +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover .hypothesis/ .pytest_cache/ From d582ba838e7598afbaa23684b3291de755e61975 Mon Sep 17 00:00:00 2001 From: Kevin Lewis Date: Thu, 27 Jun 2024 14:00:11 -0400 Subject: [PATCH 16/21] Add code coverage badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 07028c515..2f84ebdbb 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.8033939.svg)](https://doi.org/10.5281/zenodo.8033939) ![conda-package](https://github.com/HEXRD/hexrd/workflows/conda-package/badge.svg) ![test](https://github.com/HEXRD/hexrd/workflows/test/badge.svg) ![latest version](https://anaconda.org/hexrd/hexrd/badges/version.svg) ![last updated](https://anaconda.org/hexrd/hexrd/badges/latest_release_relative_date.svg) ![downloads](https://anaconda.org/hexrd/hexrd/badges/downloads.svg) +[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.8033939.svg)](https://doi.org/10.5281/zenodo.8033939) ![conda-package](https://github.com/HEXRD/hexrd/workflows/conda-package/badge.svg) ![test](https://github.com/HEXRD/hexrd/workflows/test/badge.svg) [![codecov](https://codecov.io/gh/Verdant-Evolution/hexrd/graph/badge.svg?token=UNM5X7BYDD)](https://codecov.io/gh/Verdant-Evolution/hexrd)![latest version](https://anaconda.org/hexrd/hexrd/badges/version.svg) ![last updated](https://anaconda.org/hexrd/hexrd/badges/latest_release_relative_date.svg) ![downloads](https://anaconda.org/hexrd/hexrd/badges/downloads.svg) # HEXRD The HEXRD project is developing a cross-platform, open-source library for the general analysis of X-ray diffraction data. This includes powder diffraction, Laue diffraction, and High Energy Diffraction Microscopy (_a.k.a._ 3DXRD, multi-grain rotation method) modalities. At its core, HEXRD provides an abstraction of a generic diffraction instrument with support for multiple detectors. This includes optimized transforms from the direct and reciprocal crystal lattices to the local detector coordinates, harnesses for interpolating image data into scattering angle coordinates, and sophisticated calibration routines. From 003e896def78c382d10603931f4301b9bb66c440 Mon Sep 17 00:00:00 2001 From: Kevin Lewis Date: Thu, 27 Jun 2024 14:01:22 -0400 Subject: [PATCH 17/21] add space --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2f84ebdbb..06bbdfe1d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.8033939.svg)](https://doi.org/10.5281/zenodo.8033939) ![conda-package](https://github.com/HEXRD/hexrd/workflows/conda-package/badge.svg) ![test](https://github.com/HEXRD/hexrd/workflows/test/badge.svg) [![codecov](https://codecov.io/gh/Verdant-Evolution/hexrd/graph/badge.svg?token=UNM5X7BYDD)](https://codecov.io/gh/Verdant-Evolution/hexrd)![latest version](https://anaconda.org/hexrd/hexrd/badges/version.svg) ![last updated](https://anaconda.org/hexrd/hexrd/badges/latest_release_relative_date.svg) ![downloads](https://anaconda.org/hexrd/hexrd/badges/downloads.svg) +[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.8033939.svg)](https://doi.org/10.5281/zenodo.8033939) ![conda-package](https://github.com/HEXRD/hexrd/workflows/conda-package/badge.svg) ![test](https://github.com/HEXRD/hexrd/workflows/test/badge.svg) [![codecov](https://codecov.io/gh/Verdant-Evolution/hexrd/graph/badge.svg?token=UNM5X7BYDD)](https://codecov.io/gh/Verdant-Evolution/hexrd) ![latest version](https://anaconda.org/hexrd/hexrd/badges/version.svg) ![last updated](https://anaconda.org/hexrd/hexrd/badges/latest_release_relative_date.svg) ![downloads](https://anaconda.org/hexrd/hexrd/badges/downloads.svg) # HEXRD The HEXRD project is developing a cross-platform, open-source library for the general analysis of X-ray diffraction data. This includes powder diffraction, Laue diffraction, and High Energy Diffraction Microscopy (_a.k.a._ 3DXRD, multi-grain rotation method) modalities. At its core, HEXRD provides an abstraction of a generic diffraction instrument with support for multiple detectors. This includes optimized transforms from the direct and reciprocal crystal lattices to the local detector coordinates, harnesses for interpolating image data into scattering angle coordinates, and sophisticated calibration routines. From 1f7b1fd195bdd4df3d847529ed1f6ca10c55fae7 Mon Sep 17 00:00:00 2001 From: Kevin Lewis Date: Mon, 1 Jul 2024 09:46:35 -0400 Subject: [PATCH 18/21] Refactored two functions --- hexrd/gridutil.py | 65 ++++++++++++++------------------------ hexrd/matrixutil.py | 52 ++++++++---------------------- tests/test_matrix_utils.py | 52 ++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 81 deletions(-) create mode 100644 tests/test_matrix_utils.py diff --git a/hexrd/gridutil.py b/hexrd/gridutil.py index f9c5cb70f..e5ce4bcd7 100644 --- a/hexrd/gridutil.py +++ b/hexrd/gridutil.py @@ -93,9 +93,9 @@ def cellIndices(edges, points_1d): def _fill_connectivity(out, m, n, p): i_con = 0 for k in range(p): + extra = k*(n+1)*(m+1) for j in range(m): for i in range(n): - extra = k*(n+1)*(m+1) out[i_con, 0] = i + j*(n + 1) + 1 + extra out[i_con, 1] = i + j*(n + 1) + extra out[i_con, 2] = i + j + n*(j+1) + 1 + extra @@ -122,12 +122,11 @@ def cellConnectivity(m, n, p=1, origin='ul'): if p > 1: nele = m*n*(p-1) - tmp_con3 = con.reshape(p, m*n, 4) + tmp_con3 = con.reshape((p, m*n, 4)) hex_con = [] for layer in range(p - 1): hex_con.append(np.hstack([tmp_con3[layer], tmp_con3[layer + 1]])) con = np.vstack(hex_con) - pass if origin.lower().strip() == 'll': con = con[:, ::-1] return con @@ -152,7 +151,6 @@ def cellCentroids(crd, con): def compute_areas(xy_eval_vtx, conn): areas = np.empty(len(conn)) for i in range(len(conn)): - c0, c1, c2, c3 = conn[i] vtx0x, vtx0y = xy_eval_vtx[conn[i, 0]] vtx1x, vtx1y = xy_eval_vtx[conn[i, 1]] v0x, v0y = vtx1x-vtx0x, vtx1y-vtx0y @@ -201,24 +199,23 @@ def computeArea(polygon): triv = np.array([[[0, i - 1], [0, i]] for i in range(2, n_vertices)]) area = 0 - for i in range(len(triv)): + for [s1, s2] in triv: tvp = np.diff( - np.hstack([polygon[triv[i][0], :], - polygon[triv[i][1], :]]), axis=0).flatten() + np.hstack([polygon[s1, :], + polygon[s2, :]]), axis=0).flatten() area += 0.5 * np.cross(tvp[:2], tvp[2:]) return area def make_tolerance_grid(bin_width, window_width, num_subdivisions, adjust_window=False, one_sided=False): - if bin_width > window_width: - bin_width = window_width + bin_width = min(bin_width, window_width) if adjust_window: window_width = np.ceil(window_width/bin_width)*bin_width if one_sided: ndiv = abs(int(window_width/bin_width)) grid = (np.arange(0, 2*ndiv+1) - ndiv)*bin_width - ndiv = 2*ndiv + ndiv *= 2 else: ndiv = int(num_subdivisions*np.ceil(window_width/float(bin_width))) grid = np.arange(0, ndiv+1)*window_width/float(ndiv) - 0.5*window_width @@ -228,46 +225,33 @@ def make_tolerance_grid(bin_width, window_width, num_subdivisions, def computeIntersection(line1, line2): """ compute intersection of two-dimensional line intersection + Returns the intersection point as an array of length 2. + If the lines are parallel (or equal) the function returns an empty array. this is an implementation of two lines: - line1 = [ [x0, y0], [x1, y1] ] - line1 = [ [x3, y3], [x4, y4] ] + line1 = [ [x1, y1], [x2, y2] ] + line2 = [ [x3, y3], [x4, y4] ] """ intersection = np.zeros(2) - l1 = np.array(line1) - l2 = np.array(line2) + [x1, y1] = line1[0] + [x2, y2] = line1[1] + [x3, y3] = line2[0] + [x4, y4] = line2[1] - det_l1 = det(l1) - det_l2 = det(l2) + denom = (x1-x2)*(y3-y4) - (y1-y2)*(x3-x4) + if denom == 0: + return [] - det_l1_x = det(np.vstack([l1[:, 0], np.ones(2)]).T) - det_l1_y = det(np.vstack([l1[:, 1], np.ones(2)]).T) + subterm1 = x1*y2 - y1*x2 + subterm2 = x3*y4 - y3*x4 - det_l2_x = det(np.vstack([l2[:, 0], np.ones(2)]).T) - det_l2_y = det(np.vstack([l2[:, 1], np.ones(2)]).T) - - denominator = det( - np.vstack([[det_l1_x, det_l1_y], [det_l2_x, det_l2_y]]) - ) - - if denominator == 0: - intersection = [] - else: - intersection[0] = det( - np.vstack( - [[det_l1, det_l1_x], [det_l2, det_l2_x]] - ) - ) / denominator - intersection[1] = det( - np.vstack( - [[det_l1, det_l1_y], [det_l2, det_l2_y]] - ) - ) / denominator + intersection[0] = (subterm1*(x3-x4) - subterm2*(x1-x2)) / denom + intersection[1] = (subterm1*(y3-y4) - subterm2*(y1-y2)) / denom return intersection @@ -295,6 +279,7 @@ def isinside(point, boundary, ccw=True): def sutherlandHodgman(subjectPolygon, clipPolygon): """ + https://en.wikipedia.org/wiki/Sutherland%E2%80%93Hodgman_algorithm """ subjectPolygon = np.array(subjectPolygon) clipPolygon = np.array(clipPolygon) @@ -332,7 +317,6 @@ def sutherlandHodgman(subjectPolygon, clipPolygon): outputList.append( computeIntersection(subjectLineSegment, clipBoundary) ) - pass outputList.append(curr_subjectVertex) elif isinside(prev_subjectVertex, clipBoundary): subjectLineSegment = np.vstack( @@ -341,9 +325,6 @@ def sutherlandHodgman(subjectPolygon, clipPolygon): outputList.append( computeIntersection(subjectLineSegment, clipBoundary) ) - pass prev_subjectVertex = curr_subjectVertex prev_clipVertex = curr_clipVertex - pass - pass return outputList diff --git a/hexrd/matrixutil.py b/hexrd/matrixutil.py index 12569a21b..b5000a109 100644 --- a/hexrd/matrixutil.py +++ b/hexrd/matrixutil.py @@ -262,42 +262,19 @@ def vecMVCOBMatrix(R): T = np.zeros((nrot, 6, 6), dtype='float64') - T[:, 0, 0] = R[:, 0, 0]**2 - T[:, 0, 1] = R[:, 0, 1]**2 - T[:, 0, 2] = R[:, 0, 2]**2 - T[:, 0, 3] = sqr2 * R[:, 0, 1] * R[:, 0, 2] - T[:, 0, 4] = sqr2 * R[:, 0, 0] * R[:, 0, 2] - T[:, 0, 5] = sqr2 * R[:, 0, 0] * R[:, 0, 1] - T[:, 1, 0] = R[:, 1, 0]**2 - T[:, 1, 1] = R[:, 1, 1]**2 - T[:, 1, 2] = R[:, 1, 2]**2 - T[:, 1, 3] = sqr2 * R[:, 1, 1] * R[:, 1, 2] - T[:, 1, 4] = sqr2 * R[:, 1, 0] * R[:, 1, 2] - T[:, 1, 5] = sqr2 * R[:, 1, 0] * R[:, 1, 1] - T[:, 2, 0] = R[:, 2, 0]**2 - T[:, 2, 1] = R[:, 2, 1]**2 - T[:, 2, 2] = R[:, 2, 2]**2 - T[:, 2, 3] = sqr2 * R[:, 2, 1] * R[:, 2, 2] - T[:, 2, 4] = sqr2 * R[:, 2, 0] * R[:, 2, 2] - T[:, 2, 5] = sqr2 * R[:, 2, 0] * R[:, 2, 1] - T[:, 3, 0] = sqr2 * R[:, 1, 0] * R[:, 2, 0] - T[:, 3, 1] = sqr2 * R[:, 1, 1] * R[:, 2, 1] - T[:, 3, 2] = sqr2 * R[:, 1, 2] * R[:, 2, 2] - T[:, 3, 3] = R[:, 1, 2] * R[:, 2, 1] + R[:, 1, 1] * R[:, 2, 2] - T[:, 3, 4] = R[:, 1, 2] * R[:, 2, 0] + R[:, 1, 0] * R[:, 2, 2] - T[:, 3, 5] = R[:, 1, 1] * R[:, 2, 0] + R[:, 1, 0] * R[:, 2, 1] - T[:, 4, 0] = sqr2 * R[:, 0, 0] * R[:, 2, 0] - T[:, 4, 1] = sqr2 * R[:, 0, 1] * R[:, 2, 1] - T[:, 4, 2] = sqr2 * R[:, 0, 2] * R[:, 2, 2] - T[:, 4, 3] = R[:, 0, 2] * R[:, 2, 1] + R[:, 0, 1] * R[:, 2, 2] - T[:, 4, 4] = R[:, 0, 2] * R[:, 2, 0] + R[:, 0, 0] * R[:, 2, 2] - T[:, 4, 5] = R[:, 0, 1] * R[:, 2, 0] + R[:, 0, 0] * R[:, 2, 1] - T[:, 5, 0] = sqr2 * R[:, 0, 0] * R[:, 1, 0] - T[:, 5, 1] = sqr2 * R[:, 0, 1] * R[:, 1, 1] - T[:, 5, 2] = sqr2 * R[:, 0, 2] * R[:, 1, 2] - T[:, 5, 3] = R[:, 0, 2] * R[:, 1, 1] + R[:, 0, 1] * R[:, 1, 2] - T[:, 5, 4] = R[:, 0, 0] * R[:, 1, 2] + R[:, 0, 2] * R[:, 1, 0] - T[:, 5, 5] = R[:, 0, 1] * R[:, 1, 0] + R[:, 0, 0] * R[:, 1, 1] + for i in range(3): + # Other two i values + i1, i2 = [k for k in range(3) if k!= i] + for j in range(3): + # Other two j values + j1, j2 = [k for k in range(3) if k!= j] + + T[:, i, j] = R[:, i, j] ** 2 + T[:, i, j + 3] = sqr2 * R[:, i, j1] * R[:, i, j2] + T[:, i + 3, j] = sqr2 * R[:, i1, j] * R[:, i2, j] + T[:, i + 3, j + 3] = ( + R[:, i1, j1] * R[:, i2, j2] + R[:, i1, j2] * R[:, i2, j1] + ) if nrot == 1: T = T.squeeze() @@ -562,7 +539,6 @@ def uniqueVectors(v, tol=1.0e-12): indep = np.hstack([True, tmpcmp > tol]) # independent values rowint = indep.cumsum() iv[np.ix_([row], tmpord)] = rowint - pass # # Dictionary sort from bottom up # @@ -577,8 +553,6 @@ def uniqueVectors(v, tol=1.0e-12): if any(ivSrt[:, col] != ivSrt[:, col - 1]): ivInd[nUniq] = col nUniq += 1 - pass - pass return vSrt[:, ivInd[0:nUniq]] diff --git a/tests/test_matrix_utils.py b/tests/test_matrix_utils.py new file mode 100644 index 000000000..3f9dd6622 --- /dev/null +++ b/tests/test_matrix_utils.py @@ -0,0 +1,52 @@ +import numpy as np + +from hexrd import matrixutil as mutil + +def test_vec_mv_cob_matrix(): + np.random.seed(0) + # Generate some random matrices + R = np.random.rand(20,3,3) * 2 - 1 + + T = np.zeros((len(R), 6, 6), dtype='float64') + sqr2 = np.sqrt(2) + # Hardcoded implementation + T[:, 0, 0] = R[:, 0, 0]**2 + T[:, 0, 1] = R[:, 0, 1]**2 + T[:, 0, 2] = R[:, 0, 2]**2 + T[:, 0, 3] = sqr2 * R[:, 0, 1] * R[:, 0, 2] + T[:, 0, 4] = sqr2 * R[:, 0, 0] * R[:, 0, 2] + T[:, 0, 5] = sqr2 * R[:, 0, 0] * R[:, 0, 1] + T[:, 1, 0] = R[:, 1, 0]**2 + T[:, 1, 1] = R[:, 1, 1]**2 + T[:, 1, 2] = R[:, 1, 2]**2 + T[:, 1, 3] = sqr2 * R[:, 1, 1] * R[:, 1, 2] + T[:, 1, 4] = sqr2 * R[:, 1, 0] * R[:, 1, 2] + T[:, 1, 5] = sqr2 * R[:, 1, 0] * R[:, 1, 1] + T[:, 2, 0] = R[:, 2, 0]**2 + T[:, 2, 1] = R[:, 2, 1]**2 + T[:, 2, 2] = R[:, 2, 2]**2 + T[:, 2, 3] = sqr2 * R[:, 2, 1] * R[:, 2, 2] + T[:, 2, 4] = sqr2 * R[:, 2, 0] * R[:, 2, 2] + T[:, 2, 5] = sqr2 * R[:, 2, 0] * R[:, 2, 1] + T[:, 3, 0] = sqr2 * R[:, 1, 0] * R[:, 2, 0] + T[:, 3, 1] = sqr2 * R[:, 1, 1] * R[:, 2, 1] + T[:, 3, 2] = sqr2 * R[:, 1, 2] * R[:, 2, 2] + T[:, 3, 3] = R[:, 1, 2] * R[:, 2, 1] + R[:, 1, 1] * R[:, 2, 2] + T[:, 3, 4] = R[:, 1, 2] * R[:, 2, 0] + R[:, 1, 0] * R[:, 2, 2] + T[:, 3, 5] = R[:, 1, 1] * R[:, 2, 0] + R[:, 1, 0] * R[:, 2, 1] + T[:, 4, 0] = sqr2 * R[:, 0, 0] * R[:, 2, 0] + T[:, 4, 1] = sqr2 * R[:, 0, 1] * R[:, 2, 1] + T[:, 4, 2] = sqr2 * R[:, 0, 2] * R[:, 2, 2] + T[:, 4, 3] = R[:, 0, 2] * R[:, 2, 1] + R[:, 0, 1] * R[:, 2, 2] + T[:, 4, 4] = R[:, 0, 2] * R[:, 2, 0] + R[:, 0, 0] * R[:, 2, 2] + T[:, 4, 5] = R[:, 0, 1] * R[:, 2, 0] + R[:, 0, 0] * R[:, 2, 1] + T[:, 5, 0] = sqr2 * R[:, 0, 0] * R[:, 1, 0] + T[:, 5, 1] = sqr2 * R[:, 0, 1] * R[:, 1, 1] + T[:, 5, 2] = sqr2 * R[:, 0, 2] * R[:, 1, 2] + T[:, 5, 3] = R[:, 0, 2] * R[:, 1, 1] + R[:, 0, 1] * R[:, 1, 2] + T[:, 5, 4] = R[:, 0, 0] * R[:, 1, 2] + R[:, 0, 2] * R[:, 1, 0] + T[:, 5, 5] = R[:, 0, 1] * R[:, 1, 0] + R[:, 0, 0] * R[:, 1, 1] + + T2 = mutil.vecMVCOBMatrix(R) + + assert np.allclose(T, T2) \ No newline at end of file From 9098a3457dee91a8d71029cf8c01f05f48862a48 Mon Sep 17 00:00:00 2001 From: Kevin Lewis Date: Mon, 1 Jul 2024 09:55:31 -0400 Subject: [PATCH 19/21] PEP8 fixes --- hexrd/matrixutil.py | 4 ++-- tests/test_matrix_utils.py | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/hexrd/matrixutil.py b/hexrd/matrixutil.py index b5000a109..d446d12e5 100644 --- a/hexrd/matrixutil.py +++ b/hexrd/matrixutil.py @@ -264,10 +264,10 @@ def vecMVCOBMatrix(R): for i in range(3): # Other two i values - i1, i2 = [k for k in range(3) if k!= i] + i1, i2 = [k for k in range(3) if k != i] for j in range(3): # Other two j values - j1, j2 = [k for k in range(3) if k!= j] + j1, j2 = [k for k in range(3) if k != j] T[:, i, j] = R[:, i, j] ** 2 T[:, i, j + 3] = sqr2 * R[:, i, j1] * R[:, i, j2] diff --git a/tests/test_matrix_utils.py b/tests/test_matrix_utils.py index 3f9dd6622..510eb0f25 100644 --- a/tests/test_matrix_utils.py +++ b/tests/test_matrix_utils.py @@ -2,10 +2,11 @@ from hexrd import matrixutil as mutil + def test_vec_mv_cob_matrix(): np.random.seed(0) # Generate some random matrices - R = np.random.rand(20,3,3) * 2 - 1 + R = np.random.rand(20, 3, 3) * 2 - 1 T = np.zeros((len(R), 6, 6), dtype='float64') sqr2 = np.sqrt(2) @@ -49,4 +50,4 @@ def test_vec_mv_cob_matrix(): T2 = mutil.vecMVCOBMatrix(R) - assert np.allclose(T, T2) \ No newline at end of file + assert np.allclose(T, T2) From 822612c910722632e69783b83f7a87812aa01f5e Mon Sep 17 00:00:00 2001 From: Kevin Lewis Date: Mon, 1 Jul 2024 09:46:35 -0400 Subject: [PATCH 20/21] Refactored two functions --- hexrd/gridutil.py | 65 ++++++++++++++------------------------ hexrd/matrixutil.py | 52 ++++++++---------------------- tests/test_matrix_utils.py | 52 ++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 81 deletions(-) create mode 100644 tests/test_matrix_utils.py diff --git a/hexrd/gridutil.py b/hexrd/gridutil.py index f9c5cb70f..e5ce4bcd7 100644 --- a/hexrd/gridutil.py +++ b/hexrd/gridutil.py @@ -93,9 +93,9 @@ def cellIndices(edges, points_1d): def _fill_connectivity(out, m, n, p): i_con = 0 for k in range(p): + extra = k*(n+1)*(m+1) for j in range(m): for i in range(n): - extra = k*(n+1)*(m+1) out[i_con, 0] = i + j*(n + 1) + 1 + extra out[i_con, 1] = i + j*(n + 1) + extra out[i_con, 2] = i + j + n*(j+1) + 1 + extra @@ -122,12 +122,11 @@ def cellConnectivity(m, n, p=1, origin='ul'): if p > 1: nele = m*n*(p-1) - tmp_con3 = con.reshape(p, m*n, 4) + tmp_con3 = con.reshape((p, m*n, 4)) hex_con = [] for layer in range(p - 1): hex_con.append(np.hstack([tmp_con3[layer], tmp_con3[layer + 1]])) con = np.vstack(hex_con) - pass if origin.lower().strip() == 'll': con = con[:, ::-1] return con @@ -152,7 +151,6 @@ def cellCentroids(crd, con): def compute_areas(xy_eval_vtx, conn): areas = np.empty(len(conn)) for i in range(len(conn)): - c0, c1, c2, c3 = conn[i] vtx0x, vtx0y = xy_eval_vtx[conn[i, 0]] vtx1x, vtx1y = xy_eval_vtx[conn[i, 1]] v0x, v0y = vtx1x-vtx0x, vtx1y-vtx0y @@ -201,24 +199,23 @@ def computeArea(polygon): triv = np.array([[[0, i - 1], [0, i]] for i in range(2, n_vertices)]) area = 0 - for i in range(len(triv)): + for [s1, s2] in triv: tvp = np.diff( - np.hstack([polygon[triv[i][0], :], - polygon[triv[i][1], :]]), axis=0).flatten() + np.hstack([polygon[s1, :], + polygon[s2, :]]), axis=0).flatten() area += 0.5 * np.cross(tvp[:2], tvp[2:]) return area def make_tolerance_grid(bin_width, window_width, num_subdivisions, adjust_window=False, one_sided=False): - if bin_width > window_width: - bin_width = window_width + bin_width = min(bin_width, window_width) if adjust_window: window_width = np.ceil(window_width/bin_width)*bin_width if one_sided: ndiv = abs(int(window_width/bin_width)) grid = (np.arange(0, 2*ndiv+1) - ndiv)*bin_width - ndiv = 2*ndiv + ndiv *= 2 else: ndiv = int(num_subdivisions*np.ceil(window_width/float(bin_width))) grid = np.arange(0, ndiv+1)*window_width/float(ndiv) - 0.5*window_width @@ -228,46 +225,33 @@ def make_tolerance_grid(bin_width, window_width, num_subdivisions, def computeIntersection(line1, line2): """ compute intersection of two-dimensional line intersection + Returns the intersection point as an array of length 2. + If the lines are parallel (or equal) the function returns an empty array. this is an implementation of two lines: - line1 = [ [x0, y0], [x1, y1] ] - line1 = [ [x3, y3], [x4, y4] ] + line1 = [ [x1, y1], [x2, y2] ] + line2 = [ [x3, y3], [x4, y4] ] """ intersection = np.zeros(2) - l1 = np.array(line1) - l2 = np.array(line2) + [x1, y1] = line1[0] + [x2, y2] = line1[1] + [x3, y3] = line2[0] + [x4, y4] = line2[1] - det_l1 = det(l1) - det_l2 = det(l2) + denom = (x1-x2)*(y3-y4) - (y1-y2)*(x3-x4) + if denom == 0: + return [] - det_l1_x = det(np.vstack([l1[:, 0], np.ones(2)]).T) - det_l1_y = det(np.vstack([l1[:, 1], np.ones(2)]).T) + subterm1 = x1*y2 - y1*x2 + subterm2 = x3*y4 - y3*x4 - det_l2_x = det(np.vstack([l2[:, 0], np.ones(2)]).T) - det_l2_y = det(np.vstack([l2[:, 1], np.ones(2)]).T) - - denominator = det( - np.vstack([[det_l1_x, det_l1_y], [det_l2_x, det_l2_y]]) - ) - - if denominator == 0: - intersection = [] - else: - intersection[0] = det( - np.vstack( - [[det_l1, det_l1_x], [det_l2, det_l2_x]] - ) - ) / denominator - intersection[1] = det( - np.vstack( - [[det_l1, det_l1_y], [det_l2, det_l2_y]] - ) - ) / denominator + intersection[0] = (subterm1*(x3-x4) - subterm2*(x1-x2)) / denom + intersection[1] = (subterm1*(y3-y4) - subterm2*(y1-y2)) / denom return intersection @@ -295,6 +279,7 @@ def isinside(point, boundary, ccw=True): def sutherlandHodgman(subjectPolygon, clipPolygon): """ + https://en.wikipedia.org/wiki/Sutherland%E2%80%93Hodgman_algorithm """ subjectPolygon = np.array(subjectPolygon) clipPolygon = np.array(clipPolygon) @@ -332,7 +317,6 @@ def sutherlandHodgman(subjectPolygon, clipPolygon): outputList.append( computeIntersection(subjectLineSegment, clipBoundary) ) - pass outputList.append(curr_subjectVertex) elif isinside(prev_subjectVertex, clipBoundary): subjectLineSegment = np.vstack( @@ -341,9 +325,6 @@ def sutherlandHodgman(subjectPolygon, clipPolygon): outputList.append( computeIntersection(subjectLineSegment, clipBoundary) ) - pass prev_subjectVertex = curr_subjectVertex prev_clipVertex = curr_clipVertex - pass - pass return outputList diff --git a/hexrd/matrixutil.py b/hexrd/matrixutil.py index 12569a21b..b5000a109 100644 --- a/hexrd/matrixutil.py +++ b/hexrd/matrixutil.py @@ -262,42 +262,19 @@ def vecMVCOBMatrix(R): T = np.zeros((nrot, 6, 6), dtype='float64') - T[:, 0, 0] = R[:, 0, 0]**2 - T[:, 0, 1] = R[:, 0, 1]**2 - T[:, 0, 2] = R[:, 0, 2]**2 - T[:, 0, 3] = sqr2 * R[:, 0, 1] * R[:, 0, 2] - T[:, 0, 4] = sqr2 * R[:, 0, 0] * R[:, 0, 2] - T[:, 0, 5] = sqr2 * R[:, 0, 0] * R[:, 0, 1] - T[:, 1, 0] = R[:, 1, 0]**2 - T[:, 1, 1] = R[:, 1, 1]**2 - T[:, 1, 2] = R[:, 1, 2]**2 - T[:, 1, 3] = sqr2 * R[:, 1, 1] * R[:, 1, 2] - T[:, 1, 4] = sqr2 * R[:, 1, 0] * R[:, 1, 2] - T[:, 1, 5] = sqr2 * R[:, 1, 0] * R[:, 1, 1] - T[:, 2, 0] = R[:, 2, 0]**2 - T[:, 2, 1] = R[:, 2, 1]**2 - T[:, 2, 2] = R[:, 2, 2]**2 - T[:, 2, 3] = sqr2 * R[:, 2, 1] * R[:, 2, 2] - T[:, 2, 4] = sqr2 * R[:, 2, 0] * R[:, 2, 2] - T[:, 2, 5] = sqr2 * R[:, 2, 0] * R[:, 2, 1] - T[:, 3, 0] = sqr2 * R[:, 1, 0] * R[:, 2, 0] - T[:, 3, 1] = sqr2 * R[:, 1, 1] * R[:, 2, 1] - T[:, 3, 2] = sqr2 * R[:, 1, 2] * R[:, 2, 2] - T[:, 3, 3] = R[:, 1, 2] * R[:, 2, 1] + R[:, 1, 1] * R[:, 2, 2] - T[:, 3, 4] = R[:, 1, 2] * R[:, 2, 0] + R[:, 1, 0] * R[:, 2, 2] - T[:, 3, 5] = R[:, 1, 1] * R[:, 2, 0] + R[:, 1, 0] * R[:, 2, 1] - T[:, 4, 0] = sqr2 * R[:, 0, 0] * R[:, 2, 0] - T[:, 4, 1] = sqr2 * R[:, 0, 1] * R[:, 2, 1] - T[:, 4, 2] = sqr2 * R[:, 0, 2] * R[:, 2, 2] - T[:, 4, 3] = R[:, 0, 2] * R[:, 2, 1] + R[:, 0, 1] * R[:, 2, 2] - T[:, 4, 4] = R[:, 0, 2] * R[:, 2, 0] + R[:, 0, 0] * R[:, 2, 2] - T[:, 4, 5] = R[:, 0, 1] * R[:, 2, 0] + R[:, 0, 0] * R[:, 2, 1] - T[:, 5, 0] = sqr2 * R[:, 0, 0] * R[:, 1, 0] - T[:, 5, 1] = sqr2 * R[:, 0, 1] * R[:, 1, 1] - T[:, 5, 2] = sqr2 * R[:, 0, 2] * R[:, 1, 2] - T[:, 5, 3] = R[:, 0, 2] * R[:, 1, 1] + R[:, 0, 1] * R[:, 1, 2] - T[:, 5, 4] = R[:, 0, 0] * R[:, 1, 2] + R[:, 0, 2] * R[:, 1, 0] - T[:, 5, 5] = R[:, 0, 1] * R[:, 1, 0] + R[:, 0, 0] * R[:, 1, 1] + for i in range(3): + # Other two i values + i1, i2 = [k for k in range(3) if k!= i] + for j in range(3): + # Other two j values + j1, j2 = [k for k in range(3) if k!= j] + + T[:, i, j] = R[:, i, j] ** 2 + T[:, i, j + 3] = sqr2 * R[:, i, j1] * R[:, i, j2] + T[:, i + 3, j] = sqr2 * R[:, i1, j] * R[:, i2, j] + T[:, i + 3, j + 3] = ( + R[:, i1, j1] * R[:, i2, j2] + R[:, i1, j2] * R[:, i2, j1] + ) if nrot == 1: T = T.squeeze() @@ -562,7 +539,6 @@ def uniqueVectors(v, tol=1.0e-12): indep = np.hstack([True, tmpcmp > tol]) # independent values rowint = indep.cumsum() iv[np.ix_([row], tmpord)] = rowint - pass # # Dictionary sort from bottom up # @@ -577,8 +553,6 @@ def uniqueVectors(v, tol=1.0e-12): if any(ivSrt[:, col] != ivSrt[:, col - 1]): ivInd[nUniq] = col nUniq += 1 - pass - pass return vSrt[:, ivInd[0:nUniq]] diff --git a/tests/test_matrix_utils.py b/tests/test_matrix_utils.py new file mode 100644 index 000000000..3f9dd6622 --- /dev/null +++ b/tests/test_matrix_utils.py @@ -0,0 +1,52 @@ +import numpy as np + +from hexrd import matrixutil as mutil + +def test_vec_mv_cob_matrix(): + np.random.seed(0) + # Generate some random matrices + R = np.random.rand(20,3,3) * 2 - 1 + + T = np.zeros((len(R), 6, 6), dtype='float64') + sqr2 = np.sqrt(2) + # Hardcoded implementation + T[:, 0, 0] = R[:, 0, 0]**2 + T[:, 0, 1] = R[:, 0, 1]**2 + T[:, 0, 2] = R[:, 0, 2]**2 + T[:, 0, 3] = sqr2 * R[:, 0, 1] * R[:, 0, 2] + T[:, 0, 4] = sqr2 * R[:, 0, 0] * R[:, 0, 2] + T[:, 0, 5] = sqr2 * R[:, 0, 0] * R[:, 0, 1] + T[:, 1, 0] = R[:, 1, 0]**2 + T[:, 1, 1] = R[:, 1, 1]**2 + T[:, 1, 2] = R[:, 1, 2]**2 + T[:, 1, 3] = sqr2 * R[:, 1, 1] * R[:, 1, 2] + T[:, 1, 4] = sqr2 * R[:, 1, 0] * R[:, 1, 2] + T[:, 1, 5] = sqr2 * R[:, 1, 0] * R[:, 1, 1] + T[:, 2, 0] = R[:, 2, 0]**2 + T[:, 2, 1] = R[:, 2, 1]**2 + T[:, 2, 2] = R[:, 2, 2]**2 + T[:, 2, 3] = sqr2 * R[:, 2, 1] * R[:, 2, 2] + T[:, 2, 4] = sqr2 * R[:, 2, 0] * R[:, 2, 2] + T[:, 2, 5] = sqr2 * R[:, 2, 0] * R[:, 2, 1] + T[:, 3, 0] = sqr2 * R[:, 1, 0] * R[:, 2, 0] + T[:, 3, 1] = sqr2 * R[:, 1, 1] * R[:, 2, 1] + T[:, 3, 2] = sqr2 * R[:, 1, 2] * R[:, 2, 2] + T[:, 3, 3] = R[:, 1, 2] * R[:, 2, 1] + R[:, 1, 1] * R[:, 2, 2] + T[:, 3, 4] = R[:, 1, 2] * R[:, 2, 0] + R[:, 1, 0] * R[:, 2, 2] + T[:, 3, 5] = R[:, 1, 1] * R[:, 2, 0] + R[:, 1, 0] * R[:, 2, 1] + T[:, 4, 0] = sqr2 * R[:, 0, 0] * R[:, 2, 0] + T[:, 4, 1] = sqr2 * R[:, 0, 1] * R[:, 2, 1] + T[:, 4, 2] = sqr2 * R[:, 0, 2] * R[:, 2, 2] + T[:, 4, 3] = R[:, 0, 2] * R[:, 2, 1] + R[:, 0, 1] * R[:, 2, 2] + T[:, 4, 4] = R[:, 0, 2] * R[:, 2, 0] + R[:, 0, 0] * R[:, 2, 2] + T[:, 4, 5] = R[:, 0, 1] * R[:, 2, 0] + R[:, 0, 0] * R[:, 2, 1] + T[:, 5, 0] = sqr2 * R[:, 0, 0] * R[:, 1, 0] + T[:, 5, 1] = sqr2 * R[:, 0, 1] * R[:, 1, 1] + T[:, 5, 2] = sqr2 * R[:, 0, 2] * R[:, 1, 2] + T[:, 5, 3] = R[:, 0, 2] * R[:, 1, 1] + R[:, 0, 1] * R[:, 1, 2] + T[:, 5, 4] = R[:, 0, 0] * R[:, 1, 2] + R[:, 0, 2] * R[:, 1, 0] + T[:, 5, 5] = R[:, 0, 1] * R[:, 1, 0] + R[:, 0, 0] * R[:, 1, 1] + + T2 = mutil.vecMVCOBMatrix(R) + + assert np.allclose(T, T2) \ No newline at end of file From dc7c180ee2a4d27fe377b4ae779dc6ce612ed281 Mon Sep 17 00:00:00 2001 From: Kevin Lewis Date: Mon, 1 Jul 2024 09:55:31 -0400 Subject: [PATCH 21/21] PEP8 fixes --- hexrd/matrixutil.py | 4 ++-- tests/test_matrix_utils.py | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/hexrd/matrixutil.py b/hexrd/matrixutil.py index b5000a109..d446d12e5 100644 --- a/hexrd/matrixutil.py +++ b/hexrd/matrixutil.py @@ -264,10 +264,10 @@ def vecMVCOBMatrix(R): for i in range(3): # Other two i values - i1, i2 = [k for k in range(3) if k!= i] + i1, i2 = [k for k in range(3) if k != i] for j in range(3): # Other two j values - j1, j2 = [k for k in range(3) if k!= j] + j1, j2 = [k for k in range(3) if k != j] T[:, i, j] = R[:, i, j] ** 2 T[:, i, j + 3] = sqr2 * R[:, i, j1] * R[:, i, j2] diff --git a/tests/test_matrix_utils.py b/tests/test_matrix_utils.py index 3f9dd6622..510eb0f25 100644 --- a/tests/test_matrix_utils.py +++ b/tests/test_matrix_utils.py @@ -2,10 +2,11 @@ from hexrd import matrixutil as mutil + def test_vec_mv_cob_matrix(): np.random.seed(0) # Generate some random matrices - R = np.random.rand(20,3,3) * 2 - 1 + R = np.random.rand(20, 3, 3) * 2 - 1 T = np.zeros((len(R), 6, 6), dtype='float64') sqr2 = np.sqrt(2) @@ -49,4 +50,4 @@ def test_vec_mv_cob_matrix(): T2 = mutil.vecMVCOBMatrix(R) - assert np.allclose(T, T2) \ No newline at end of file + assert np.allclose(T, T2)