Skip to content

Conversation

MohamedAlmaki
Copy link
Contributor

@MohamedAlmaki MohamedAlmaki commented Mar 17, 2025

Description of work

This PR fixes a bug encountered when exporting pixel cuts from MDHisto workspaces. The issue arose because the export_pixel_cut_to_workspace is assigned to the MDEvent version in the model. I have added a new function for performing pixel cuts from MDHisto workspaces. Additionally, I have added unit tests for the export pixel cut functions.

Fixes #36084.

To test:

1- Make the workspace md_3D_histo

md_4D = CreateMDWorkspace(Dimensions=4, Extents=[0,2,-1,1,-1.5,1.5,-0.25,0.25], Names="H,K,L,E", Frames='HKL,HKL,HKL,General Frame',Units='r.l.u.,r.l.u.,r.l.u.,meV')
FakeMDEventData(InputWorkspace=md_4D, UniformParams='5e5') # 4D data
FakeMDEventData(InputWorkspace=md_4D, UniformParams='1e5,0.5,1.5,-0.5,0.5,-0.75,0.75,-0.1,0.1') # 4D data

# Add a non-orthogonal UB
expt_info = CreateSampleWorkspace()
md_4D.addExperimentInfo(expt_info)
SetUB(Workspace='md_4D', c=2, gamma=120)

md_3D_histo = BinMD(InputWorkspace='md_4D', AlignedDim0='H,-2,2,100', AlignedDim1='K,-1,1,100', AlignedDim2='L,-1.5,1.5,100')

2- Open md_3D_histo in sliceviewer
3- Select the plot option
4- Press 'x' or 'y' to create a new cut along that axis
5- Check if the cut is correct


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.

@MohamedAlmaki MohamedAlmaki added Bug Issues and pull requests that are regressions or would be considered a bug by users (e.g. crashing) Diffraction Issues and pull requests related to diffraction ISIS: Diffraction Issue and pull requests relating to Diffraction at ISIS labels Mar 17, 2025
@github-project-automation github-project-automation bot moved this to Unassigned in DEVS Mar 17, 2025
@MohamedAlmaki MohamedAlmaki added this to the Release 6.13 milestone Mar 17, 2025
@MohamedAlmaki MohamedAlmaki marked this pull request as ready for review March 17, 2025 11:18
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 the comprehensive tests!

Testing this manually there does appear to be a problem
image
The exported pixle cuts just look like straight lines because there's only two bins (you can see this by looking at mtd['md_3D_histo_cut_y].getSingalArray()`

Also if there is an MDEvent workspace e.g. ws.hasOriginalWorkspace() = True then we might want to call the MDEvent function which I think should go an call BinMD on the MDEvent workspace underneath (if it has same number of dimensions - i.e. not in this case as original workspace is 4D)? What do you think?

@github-project-automation github-project-automation bot moved this from Unassigned to Waiting response in DEVS Mar 18, 2025
@RichardWaiteSTFC
Copy link
Contributor

So for the MDEvent workspace the pixel cuts exported are cuts - but they don't look much like the cuts in the interface!
It looks to me like the integration limits are wrong - this is screenshot for a cut centred on (H,K) ~ (1,0.6)

image

It looks like the integration limits for the y-cut (shown in the workspace history on LHS) is -0.385 < K < 1.61 where it should be +/- some small amount around 0.6...

@MohamedAlmaki
Copy link
Contributor Author

MohamedAlmaki commented Mar 19, 2025

So I fixed the plots, the problem was previously the integration limits were just one bin across both two dimensions - I thought that was correct as the name suggests it will extract one pixel, not a 1D slice. In addition, there was a problem when transposing the dimensions.
I have fixed the behaviour and it is now implemented as fixing the intended dimension and integrating along the other dimension with one bin integration limit. Here are the screenshots, let me know if the plots are correct (there is a slight difference as the cursor is sensitive to movement)
Cuts
MD_cuts

@RichardWaiteSTFC
Copy link
Contributor

Great thanks! Plots look good, but the only way to know for certain is by checking the workspace history and the integration limits passed. I can check this tomorrow and re-review!

@RichardWaiteSTFC
Copy link
Contributor

RichardWaiteSTFC commented Mar 20, 2025

Tested this - the y-cut looks good, but the x-cut still only has 2 points.
If I look at the call to IntegrateMDHisto for the x-cut for a cut centred on (x,y)=(H,K) ~ (1.3,0.6)

IntegrateMDHistoWorkspace(InputWorkspace='md_3D_histo', 
                          P1Bin='1.27996,0,1.31996', 
                          P2Bin='-1,1', 
                          P3Bin='-3.35276e-08,0.03', 
                          OutputWorkspace='md_3D_histo_cut_x')

and y-cut

IntegrateMDHistoWorkspace(InputWorkspace='md_3D_histo', 
                          P1Bin='1.27996,1.31996', 
                          P2Bin='-1,0,1', 
                          P3Bin='-3.35276e-08,0.03', 
                          OutputWorkspace='md_3D_histo_cut_y')

I think the x-cut (along H) should have P1Bin='-2,0,2', P2Bin=0.6-small,0.6+small

@MohamedAlmaki
Copy link
Contributor Author

MohamedAlmaki commented Mar 20, 2025

Hmmm, are you sure you have pulled the latest changes, here are my results for X cut and they have the same correct values you mentioned:
image

@RichardWaiteSTFC
Copy link
Contributor

RichardWaiteSTFC commented Mar 20, 2025

Hmm I appear to have it in the log
image
I'll check again!

@RichardWaiteSTFC
Copy link
Contributor

Ah x cut seems to be OK if I export by pressing x-key, but not c-key on keyboard!

@MohamedAlmaki
Copy link
Contributor Author

Ohh now I can reproduce it, seems a problem with c key, I will fix it

@MohamedAlmaki
Copy link
Contributor Author

Exporting both cuts using C key should work well now

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 - it looks like export_pixel_cut_to_workspace_mdhist and export_pixel_cut_to_workspace_mdevent are basically the same just calling a different function to do the actual cut (which calls different algorithms) is that right? Could we get away with one export_pixel_cut_to_workspace_md which calls a class method which is wrapper around either self.export_cuts_to_workspace_mdhisto or self.export_cuts_to_workspace_mdevent` depending on workspace type?

@MohamedAlmaki
Copy link
Contributor Author

Yeah, there is a lot of duplicate code, I will merge them into one function as you suggest

@RichardWaiteSTFC
Copy link
Contributor

Think there's still issues here - if you view (X,Y) = (L,K) the cuts still only contain one bin
image
Can you reproduce this?

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.

Great, tested on MDEvent and MDHisto with transpose in 4D and 3D and all looking good - thanks for getting this over the line!

@github-project-automation github-project-automation bot moved this from Waiting response to Approved in DEVS May 9, 2025
@jclarkeSTFC jclarkeSTFC self-assigned this May 12, 2025
Copy link
Contributor

@jclarkeSTFC jclarkeSTFC left a comment

Choose a reason for hiding this comment

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

Problem fixed, works fine. Just one question about one of the tests.

mock_extract_roi.assert_called_once_with(self.ws2d_histo, None, None, xpos, xpos, False, "ws2d_histo_cut_x")
self.assertEqual("Cut along Y created: ws2d_histo_cut_x", help_msg)

for cut_type in ("x", "y"):
Copy link
Contributor

Choose a reason for hiding this comment

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

Are there also supposed to be calls to assert_call_as_expected with transpose=True? Looks like the logic is there to handle it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ohh thanks for spotting that, yeah will add transpose true cases

@github-project-automation github-project-automation bot moved this from Approved to Waiting response in DEVS May 12, 2025
Copy link
Contributor

@jclarkeSTFC jclarkeSTFC 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 fixing the test.

@github-project-automation github-project-automation bot moved this from Waiting response to Approved in DEVS May 13, 2025
@jclarkeSTFC jclarkeSTFC enabled auto-merge (squash) May 13, 2025 13:41
@jclarkeSTFC jclarkeSTFC merged commit 7fc77c8 into main May 13, 2025
10 checks passed
@jclarkeSTFC jclarkeSTFC deleted the 36084_fix_mdhisto branch May 13, 2025 21:14
@github-project-automation github-project-automation bot moved this from Approved to Merged in DEVS May 13, 2025
peterfpeterson pushed a commit to peterfpeterson/mantid that referenced this pull request May 14, 2025
…paces (mantidproject#39071)

### Description of work
This PR fixes a bug encountered when exporting pixel cuts from MDHisto
workspaces. The issue arose because the `export_pixel_cut_to_workspace`
is assigned to the MDEvent version in the model. I have added a new
function for performing pixel cuts from MDHisto workspaces.
Additionally, I have added unit tests for the export pixel cut
functions.

<!-- Why has this work been done? If there is no linked issue please
provide appropriate context for this work.
#### Purpose of work
This can be removed if a github issue is referenced below
-->

Fixes mantidproject#36084. <!-- and fix #xxxx or close #xxxx xor resolves #xxxx. One
line per issue fixed. -->

### To test:
1- Make the workspace `md_3D_histo`
```
md_4D = CreateMDWorkspace(Dimensions=4, Extents=[0,2,-1,1,-1.5,1.5,-0.25,0.25], Names="H,K,L,E", Frames='HKL,HKL,HKL,General Frame',Units='r.l.u.,r.l.u.,r.l.u.,meV')
FakeMDEventData(InputWorkspace=md_4D, UniformParams='5e5') # 4D data
FakeMDEventData(InputWorkspace=md_4D, UniformParams='1e5,0.5,1.5,-0.5,0.5,-0.75,0.75,-0.1,0.1') # 4D data

# Add a non-orthogonal UB
expt_info = CreateSampleWorkspace()
md_4D.addExperimentInfo(expt_info)
SetUB(Workspace='md_4D', c=2, gamma=120)

md_3D_histo = BinMD(InputWorkspace='md_4D', AlignedDim0='H,-2,2,100', AlignedDim1='K,-1,1,100', AlignedDim2='L,-1.5,1.5,100')
```
2- Open `md_3D_histo` in sliceviewer
3- Select the plot option
4- Press 'x' or 'y' to create a new cut along that axis
5- Check if the cut is correct


<!-- Instructions for testing.
There should be sufficient instructions for someone unfamiliar with the
application to test - unless a specific
reviewer is requested.
If instructions for replicating the fault are contained in the linked
issue then it is OK to refer back to these.
-->

<!-- delete this if you added release notes
*This does not require release notes* because **fill in an explanation
of why**
If you add release notes please save them as a separate file using the
Issue or PR number as the file name. Check the file is located in the
correct directory for your note(s).
-->

<!-- Ensure the base of this PR is correct (e.g. release-next or main)
Finally, don't forget to add the appropriate labels, milestones, etc.!
-->

---

### Reviewer

Please comment on the points listed below ([full
description](http://developer.mantidproject.org/ReviewingAPullRequest.html)).
**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](http://developer.mantidproject.org/Standards/)?
- Are the unit tests small and test the class in isolation?
- If there is GUI work does it follow the [GUI
standards](http://developer.mantidproject.org/Standards/GUIStandards.html)?
- 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](https://developer.mantidproject.org/Standards/ReleaseNotesGuide.html)?

#### 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.
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) Diffraction Issues and pull requests related to diffraction ISIS: Diffraction Issue and pull requests relating to Diffraction at ISIS
Projects
Status: Merged
Development

Successfully merging this pull request may close these issues.

Bugs in exporting cuts from MDHisto workspaces in sliceviewer
3 participants