Skip to content

Conversation

MialLewis
Copy link
Contributor

@MialLewis MialLewis commented Apr 24, 2025

Description of work

Reported by a user, possible to provide an n argument to nthTime or nthInterval method of a FilteredTimeSeriesProperty and get an out of bounds error.

This PR adds bounds checking.

To test:

Ensure instrument is HIFI and data archive access is available.

Ensure error is handled when running the following script:

from mantid.simpleapi import *

wstuple=LoadMuonNexus(filename="196907")
ws=wstuple[0]
log=ws.getRun().getLogData("Field_Main")
log.nthTime(109)

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.

@MialLewis MialLewis marked this pull request as ready for review April 24, 2025 11:09
@sf1919 sf1919 added this to the Release 6.13 milestone Apr 25, 2025
@jhaigh0 jhaigh0 self-assigned this Apr 25, 2025
Copy link
Contributor

@jhaigh0 jhaigh0 left a comment

Choose a reason for hiding this comment

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

Just one change for the test, otherwise works as intendended. output I go from running the script:

LoadMuonNexus started
Geometry cache is not available
Creating cache in C:\Users\bya67386\AppData\Roaming\mantidproject\mantid\instrument\geometryCache\HIFI7a5093e08ddd5090d58abd7fc3aab4d5cc1547e4.vtp
Sample Log "Field_Hall_Z" contains invalid values, click "Show Sample Logs" for details.
Sample Log "Field_Hall_Z" and 0 other  contain invalid values, click "Show Sample Logs" for details.
Log "running" has 36 entries removed due to duplicated time. 
LoadMuonNexus successful, Duration 3.30 seconds
RuntimeError: nthInterval(): FilteredTimeSeriesProperty 'Field_Main' interval 109 does not exist
  File "<string>", line 6, in <module>

Comment on lines 157 to 165
// Throw exception if out of bounds
if (n >= static_cast<int>(this->m_filterIntervals.size())) {
const std::string error("nthInterval(): FilteredTimeSeriesProperty '" + this->name() + "' interval " +
std::to_string(n) + " does not exist");
g_log.debug(error);
throw std::runtime_error(error);
} else {
deltaT = this->m_filterIntervals[std::size_t(n)];
}
Copy link
Contributor

Choose a reason for hiding this comment

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

You could do this with at(), supposed to be a safe way to access vectors, although it still throws an exception?
https://en.cppreference.com/w/cpp/container/array/at
Just letting you know about it, the code is fine as it is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I think we want the custom error message in this case, in keeping with other error handling within the function.

@MialLewis MialLewis force-pushed the 0_add_bounds_check_to_nthTime branch from 702a794 to 384af75 Compare April 25, 2025 12:31
@MialLewis MialLewis requested a review from jhaigh0 April 25, 2025 12:33
@cailafinn cailafinn self-assigned this Apr 28, 2025
@cailafinn cailafinn merged commit 51acd3f into main Apr 28, 2025
10 checks passed
@cailafinn cailafinn deleted the 0_add_bounds_check_to_nthTime branch April 28, 2025 12:31
glass-ships pushed a commit that referenced this pull request Apr 28, 2025
#39238)

* throw error if out of bounds in nthIterval

* add release note

* update nthInterval test

* satisfy cppcheck - remove std::move to allow for nrvo

* cast size_t to int to resolve compiler warnings

* respond to review comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants