Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Oct 31, 2024
1 parent 4cdc6bb commit 95efe61
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ class MANTID_ALGORITHMS_DLL CreateMonteCarloWorkspace : public API::Algorithm {
const std::string category() const override;
const std::string summary() const override;


private:
void init() override;
void exec() override;

void fillHistogramWithRandomData(auto &outputY, const std::vector<double> &cdf, int numIterations, std::mt19937 &gen);
vector<double> computeNormalizedCDF(const auto &yData);
int computeNumberOfIterations(const auto &yData);

};

} // namespace Algorithms
Expand Down
39 changes: 18 additions & 21 deletions Framework/Algorithms/src/CreateMonteCarloWorkspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,36 +63,34 @@ void CreateMonteCarloWorkspace::init() {

void CreateMonteCarloWorkspace::fillHistogramWithRandomData(auto &outputY, const std::vector<double> &cdf,
int numIterations, std::mt19937 &gen) {
std::uniform_real_distribution<> dis(0.0, 1.0);
for (int i = 0; i < numIterations; ++i) {
double randomNum = dis(gen);
// Find the bin corresponding to the random number in the CDF
auto it = std::lower_bound(cdf.begin(), cdf.end(), randomNum);
size_t index = std::distance(cdf.begin(), it);

// Ensure the index is within bounds
if (index < outputY.size()) {
outputY[index] += 1.0;
}
std::uniform_real_distribution<> dis(0.0, 1.0);
for (int i = 0; i < numIterations; ++i) {
double randomNum = dis(gen);
// Find the bin corresponding to the random number in the CDF
auto it = std::lower_bound(cdf.begin(), cdf.end(), randomNum);
size_t index = std::distance(cdf.begin(), it);

// Ensure the index is within bounds
if (index < outputY.size()) {
outputY[index] += 1.0;
}
}
}

vector<double> CreateMonteCarloWorkspace::computeNormalizedCDF(const auto &yData) {
std::vector<double> cdf(yData.size());
std::partial_sum(yData.begin(), yData.end(), cdf.begin());
double total_counts = cdf.back();
// Normalize the CDF
std::transform(cdf.begin(), cdf.end(), cdf.begin(),
[total_counts](double val) { return val / total_counts; });
return cdf;
std::vector<double> cdf(yData.size());
std::partial_sum(yData.begin(), yData.end(), cdf.begin());
double total_counts = cdf.back();
// Normalize the CDF
std::transform(cdf.begin(), cdf.end(), cdf.begin(), [total_counts](double val) { return val / total_counts; });
return cdf;
}

int CreateMonteCarloWorkspace::computeNumberOfIterations(const auto &yData) {
int CreateMonteCarloWorkspace::computeNumberOfIterations(const auto &yData) {
double total_counts = std::accumulate(yData.begin(), yData.end(), 0.0);
return static_cast<int>(std::round(total_counts));
}


//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
Expand Down Expand Up @@ -124,6 +122,5 @@ void CreateMonteCarloWorkspace::exec() {
setProperty("OutputWorkspace", outputWs);
}


} // namespace Algorithms
} // namespace Mantid
7 changes: 2 additions & 5 deletions Framework/Algorithms/test/CreateMonteCarloWorkspaceTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once

#include <cxxtest/TestSuite.h>
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidAlgorithms/CreateSampleWorkspace.h"
#include "MantidAlgorithms/CreateMonteCarloWorkspace.h"
#include "MantidAlgorithms/CreateSampleWorkspace.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidFrameworkTestHelpers/WorkspaceCreationHelper.h"

#include <cxxtest/TestSuite.h>

using namespace Mantid::Algorithms;
using namespace Mantid::API;
Expand Down Expand Up @@ -70,5 +68,4 @@ class CreateMonteCarloWorkspaceTest : public CxxTest::TestSuite {

TS_FAIL("TODO: Remove this line once the test works as expected.");
}

};
1 change: 0 additions & 1 deletion docs/source/algorithms/CreateMonteCarloWorkspace-v1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,3 @@ Output:
.. categories::

.. sourcelink::

0 comments on commit 95efe61

Please sign in to comment.