Skip to content

Conversation

andy-bridger
Copy link
Collaborator

Description of work

Summary of work

When saveNexus is called by Goniometer it now saves a flattened copy of the rotation matrix

Fixes #39487

Further detail of work

Now the rotation matrix can be directly saved and loaded, which has been identified as an issue because of the new functionality to set goniometer matrices directly: #39219 (comment) but may have also been causing issues in user scripts where goniometer has been set with setR or similar

To test:

run script:

# import mantid algorithms, numpy and matplotlib
from mantid.simpleapi import *
import matplotlib.pyplot as plt
import numpy as np

cuboid = " \
<cuboid id='some-cuboid'> \
<height val='2.0'  /> \
<width val='2.0' />  \
<depth  val='0.2' />  \
<centre x='10.0' y='10.0' z='10.0'  />  \
</cuboid>  \
<algebra val='some-cuboid' /> \
"

# just set this as a folder you are happy to have some files appear
root = "C:/Users/kcd17618/Documents/MantidScripts/SaveGoniometerBug"
ws = CreateSampleWorkspace()
other_ws = CreateSampleWorkspace()

# gon mat is a 30 degree rotation around (1,1,1)            
gon_mat = np.array([[ 0.9106836,  -0.24401694,  0.33333333],
 [ 0.33333333,  0.9106836,  -0.24401694],
 [-0.24401694,  0.33333333,  0.9106836 ]])
 
# try setting the matrix directly and using an equivalent axis definition
SetGoniometer(Workspace = ws, GoniometerMatrix = gon_mat)
SetGoniometer(Workspace = other_ws, Axis0 = "30,1,1,1,1")
SetSample(ws, Geometry={'Shape': 'CSG', 'Value': cuboid})
SetSample(other_ws, Geometry={'Shape': 'CSG', 'Value': cuboid})

# verify this is the same
print("Set with GoniometerMatrix: ",ws.run().getGoniometer().getR())
print("Set with Axis0: ",other_ws.run().getGoniometer().getR())

# Save the files
SaveNexus(InputWorkspace = ws, Filename= f"{root}/tmp_ws.nxs")
SaveNexus(InputWorkspace = other_ws, Filename= f"{root}/tmp_other_ws.nxs")

# Load the files
load_ws = Load(Filename = f"{root}/tmp_ws.nxs")
load_other_ws = Load(Filename = f"{root}/tmp_other_ws.nxs")

# Results should not have changed
print("Reloaded - Set with GoniometerMatrix: ",load_ws.run().getGoniometer().getR())
print("Reloaded - Set with Axis0: ",load_other_ws.run().getGoniometer().getR())

Also worth checking that the sample is orientated correctly on all four workspaces (right click on ws, Show Sample Shape)


Reviewer

Please comment on the points listed below (full description).
Your comments will be used as part of the gatekeeper process, so please comment clearly on what you have checked during your review. If changes are made to the PR during the review process then your final comment will be the most important for gatekeepers. In this comment you should make it clear why any earlier review is still valid, or confirm that all requested changes have been addressed.

Code Review

  • Is the code of an acceptable quality?
  • Does the code conform to the coding standards?
  • Are the unit tests small and test the class in isolation?
  • If there is GUI work does it follow the GUI standards?
  • If there are changes in the release notes then do they describe the changes appropriately?
  • Do the release notes conform to the release notes guide?

Functional Tests

  • Do changes function as described? Add comments below that describe the tests performed?
  • Do the changes handle unexpected situations, e.g. bad input?
  • Has the relevant (user and developer) documentation been added/updated?

Does everything look good? Mark the review as Approve. A member of @mantidproject/gatekeepers will take care of it.

Gatekeeper

If you need to request changes to a PR then please add a comment and set the review status to "Request changes". This will stop the PR from showing up in the list for other gatekeepers.

@andy-bridger andy-bridger marked this pull request as ready for review June 16, 2025 13:24
@sf1919
Copy link
Contributor

sf1919 commented Jun 16, 2025

I think the issue is labelled v6.13 so this needs to be pointed at release-next and have appropriate milestone and labels added.

@andy-bridger andy-bridger modified the milestone: Release 6.13 Jun 16, 2025
@andy-bridger andy-bridger added Bug Issues and pull requests that are regressions or would be considered a bug by users (e.g. crashing) labels Jun 16, 2025
@andy-bridger andy-bridger changed the base branch from main to release-next June 16, 2025 13:46
@andy-bridger
Copy link
Collaborator Author

I think this could be pushed down the line to 6.14 as not technically a regression but since it is a bug that is much more accessible due to the change to SetGoniometer if this fix is suitable (doesn't break anything else) I think it is worth getting in.

Copy link
Contributor

@RichardWaiteSTFC RichardWaiteSTFC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, changes and tests look great. Test instructions worked as expected and sample shapes correctly set. I agree this deserves a release note as it preserves some precision on the saved/loaded rotation. Just one really nit-picking comment, apologies I had to find something!

file->readData("rotation_matrix", matrixData);
DblMatrix rotMat(matrixData);
setR(rotMat);
initFromR = true; // set as initFromR to prevent overwrite on R recalc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor -but I think the naming convention for such member variable (if that is the right term?) is m_initFromR - obviously this wasn't introduced in this PR (apologies for missing it before), but it helps to make the scope of the variable clear!

Copy link
Contributor

@RichardWaiteSTFC RichardWaiteSTFC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for that! Looks good to me!

@SilkeSchomann SilkeSchomann self-assigned this Jun 24, 2025
@SilkeSchomann SilkeSchomann merged commit 06cc808 into release-next Jun 24, 2025
10 checks passed
@SilkeSchomann SilkeSchomann deleted the save-goniometer-matrix branch June 24, 2025 08:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Issues and pull requests that are regressions or would be considered a bug by users (e.g. crashing)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Goniometer Matrix isn't loaded properly if set using GoniometerMatrix in SetGoniometer
4 participants