Skip to content

Commit

Permalink
Merge branch 'main' into 36322_shoebox_integration_algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardWaiteSTFC committed Dec 21, 2023
2 parents 7607686 + 12841b3 commit ce987c9
Show file tree
Hide file tree
Showing 1,133 changed files with 19,870 additions and 9,228 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repos:

# Run fast code improvement/checks before running PR specific helpers.
- repo: https://github.com/pre-commit/pre-commit-hooks.git
rev: v4.4.0
rev: v4.5.0
hooks:
- id: trailing-whitespace
args: [--markdown-linebreak-ext=md]
Expand Down Expand Up @@ -55,14 +55,14 @@ repos:
)$
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.291
rev: v0.1.7
# ruff must appear before black in the list of hooks
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]

- repo: https://github.com/psf/black
rev: 23.9.1
rev: 23.11.0
hooks:
- id: black
exclude: scripts/templates/reference/|Testing/Tools/cxxtest
5 changes: 0 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,6 @@ set(CORE_MANTIDLIBS
Mantid::Types
)

if(UNIX)
# Experimental feature. Unix only at this point.
option(UNITY_BUILD "Switch for utilising unity builds. Faster builds for selected components.")
endif(UNIX)

add_custom_target(AllTests)

if(BUILD_MANTIDFRAMEWORK)
Expand Down
18 changes: 3 additions & 15 deletions Framework/API/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,10 @@ set(SRC_FILES
src/WorkspaceNearestNeighbours.cpp
src/WorkspaceOpOverloads.cpp
src/WorkspaceProperty.cpp
src/WorkspacePropertyUtils.cpp
src/WorkspaceUnitValidator.cpp
)

set(SRC_UNITY_IGNORE_FILES
src/CompositeFunction.cpp
src/IDataFileChecker.cpp
src/MatrixWorkspace.cpp
src/IEventWorkspace.cpp
src/IFunctionMD.cpp
src/IPeakFunction.cpp
src/IPowderDiffPeakFunction.cpp
)

set(INC_FILES
inc/MantidAPI/ADSValidator.h
inc/MantidAPI/Algorithm.h
Expand Down Expand Up @@ -364,6 +355,7 @@ set(INC_FILES
inc/MantidAPI/WorkspaceOpOverloads.h
inc/MantidAPI/WorkspaceProperty.h
inc/MantidAPI/WorkspaceProperty.tcc
inc/MantidAPI/WorkspacePropertyUtils.h
inc/MantidAPI/WorkspaceUnitValidator.h
inc/MantidAPI/Workspace_fwd.h
)
Expand Down Expand Up @@ -489,6 +481,7 @@ set(TEST_FILES
WorkspaceNearestNeighboursTest.h
WorkspaceOpOverloadsTest.h
WorkspacePropertyTest.h
WorkspacePropertyUtilsTest.h
WorkspaceUnitValidatorTest.h
)

Expand All @@ -500,11 +493,6 @@ if(COVERAGE)
endforeach(loop_var)
endif()

if(UNITY_BUILD)
include(UnityBuild)
enable_unity_build(API SRC_FILES SRC_UNITY_IGNORE_FILES 10)
endif(UNITY_BUILD)

# Have to link to winsock and bcrypt library on Windows
if(WIN32)
set(WINSOCK ws2_32)
Expand Down
7 changes: 6 additions & 1 deletion Framework/API/inc/MantidAPI/AnalysisDataService.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ class MANTID_API_DLL AnalysisDataServiceImpl final : public Kernel::DataService<
/// workspace object is renamed
virtual void rename(const std::string &oldName, const std::string &newName);
/// Overridden remove member to delete its name held by the workspace itself
virtual void remove(const std::string &name);
virtual Workspace_sptr remove(const std::string &name);
/// Random generated unique workspace name
const std::string uniqueName(const int n = 5, const std::string &prefix = "", const std::string &suffix = "");
/// Random generated unique hidden workspace name
const std::string uniqueHiddenName();

/** Retrieve a workspace and cast it to the given WSTYPE
*
Expand Down Expand Up @@ -136,6 +140,7 @@ class MANTID_API_DLL AnalysisDataServiceImpl final : public Kernel::DataService<
private:
/// Checks the name is valid, throwing if not
void verifyName(const std::string &name, const std::shared_ptr<API::WorkspaceGroup> &workspace);
static char getRandomLowercaseLetter();

friend struct Mantid::Kernel::CreateUsingNew<AnalysisDataServiceImpl>;
/// Constructor
Expand Down
8 changes: 8 additions & 0 deletions Framework/API/inc/MantidAPI/IWorkspaceProperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@

namespace Mantid {
namespace API {

/// Enumeration for a mandatory/optional property
struct PropertyMode {
enum Type { Mandatory, Optional };
};

/** An interface that is implemented by WorkspaceProperty.
Used for non templated workspace operations.
Expand All @@ -28,6 +34,8 @@ class IWorkspaceProperty {
virtual void clear() = 0;
/// Get a pointer to the workspace
virtual Workspace_sptr getWorkspace() const = 0;
/// Set the property mode as Mandatory or Optional
virtual void setPropertyMode(const PropertyMode::Type &optional) = 0;
/// Is the input workspace property optional (can be blank)?
virtual bool isOptional() const = 0;
/// Will the workspace be locked when starting an algorithm?
Expand Down
6 changes: 2 additions & 4 deletions Framework/API/inc/MantidAPI/WorkspaceProperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ namespace API {
class MatrixWorkspace;
class WorkspaceGroup;

/// Enumeration for a mandatory/optional property
struct PropertyMode {
enum Type { Mandatory, Optional };
};
/// Enumeration for locking behaviour
struct LockMode {
enum Type { Lock, NoLock };
Expand Down Expand Up @@ -92,6 +88,8 @@ class WorkspaceProperty : public Kernel::PropertyWithValue<std::shared_ptr<TYPE>

std::string setDataItem(const std::shared_ptr<Kernel::DataItem> &value) override;

void setPropertyMode(const PropertyMode::Type &optional) override;

std::string isValid() const override;

bool isDefault() const override;
Expand Down
13 changes: 10 additions & 3 deletions Framework/API/inc/MantidAPI/WorkspaceProperty.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ std::string WorkspaceProperty<TYPE>::setDataItem(const std::shared_ptr<Kernel::D
return isValid();
}

/** Set the property mode of the property e.g. Mandatory or Optional
* @param optional :: Property mode Mandatory or Optional
*/
template <typename TYPE> void WorkspaceProperty<TYPE>::setPropertyMode(const PropertyMode::Type &optional) {
m_optional = optional;
}

/** Checks whether the entered workspace is valid.
* To be valid, in addition to satisfying the conditions of any validators,
* an output property must not have an empty name and an input one must point
Expand Down Expand Up @@ -388,10 +395,10 @@ std::string WorkspaceProperty<TYPE>::isValidGroup(const std::shared_ptr<Workspac
*/
template <typename TYPE> std::string WorkspaceProperty<TYPE>::isValidOutputWs() const {
std::string error;
const std::string value = this->value();
if (!value.empty()) {
const std::string workspaceName = this->value();
if (!workspaceName.empty()) {
// Will the ADS accept it
error = AnalysisDataService::Instance().isValid(value);
error = AnalysisDataService::Instance().isValid(workspaceName);
} else {
if (isOptional())
error = ""; // Optional ones don't need a name
Expand Down
21 changes: 21 additions & 0 deletions Framework/API/inc/MantidAPI/WorkspacePropertyUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright &copy; 2023 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once

#include "MantidAPI/DllConfig.h"

#include "MantidAPI/IWorkspaceProperty.h"
#include "MantidKernel/Property.h"

namespace Mantid {
namespace API {

MANTID_API_DLL void setPropertyModeForWorkspaceProperty(Mantid::Kernel::Property *prop,
const PropertyMode::Type &optional);

}
} // namespace Mantid
28 changes: 19 additions & 9 deletions Framework/API/src/Algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "MantidAPI/IWorkspaceProperty.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidAPI/WorkspaceHistory.h"
#include "MantidAPI/WorkspacePropertyUtils.h"

#include "MantidJson/Json.h"
#include "MantidKernel/CompositeValidator.h"
Expand Down Expand Up @@ -178,7 +179,16 @@ void Algorithm::enableHistoryRecordingForChild(const bool on) { m_recordHistoryF
*
* @param doStore :: always store in ADS
*/
void Algorithm::setAlwaysStoreInADS(const bool doStore) { m_alwaysStoreInADS = doStore; }
void Algorithm::setAlwaysStoreInADS(const bool doStore) {
m_alwaysStoreInADS = doStore;

// Set OutputWorkspace as an optional property in the case where alwaysStoreInADS is false. In
// this case, the output workspace name is not always required.
if (!m_alwaysStoreInADS && m_properties.existsProperty("OutputWorkspace")) {
Property *property = m_properties.getPointerToProperty("OutputWorkspace");
setPropertyModeForWorkspaceProperty(property, PropertyMode::Type::Optional);
}
}

/** Returns true if we always store in the AnalysisDataService.
* @return true if output is saved to the AnalysisDataService.
Expand Down Expand Up @@ -407,8 +417,8 @@ void Algorithm::cacheInputWorkspaceHistories() {
if (!isADSValidator(strArrayProp->getValidator()))
continue;
const auto &wsNames((*strArrayProp)());
for (const auto &name : wsNames) {
cacheHistories(ads.retrieve(name));
for (const auto &wsName : wsNames) {
cacheHistories(ads.retrieve(wsName));
}
}
}
Expand Down Expand Up @@ -975,8 +985,8 @@ IAlgorithm_sptr Algorithm::fromString(const std::string &input) {
*/
IAlgorithm_sptr Algorithm::fromJson(const Json::Value &serialized) {
const std::string algName = serialized["name"].asString();
const int version = serialized.get("version", -1).asInt();
auto alg = AlgorithmManager::Instance().createUnmanaged(algName, version);
const int versionNumber = serialized.get("version", -1).asInt();
auto alg = AlgorithmManager::Instance().createUnmanaged(algName, versionNumber);
alg->initialize();
alg->setProperties(serialized["properties"]);
return alg;
Expand Down Expand Up @@ -1146,7 +1156,7 @@ void Algorithm::logAlgorithmInfo() const {
*/
bool Algorithm::checkGroups() {
size_t numGroups = 0;
bool processGroups = false;
bool doProcessGroups = false;

// Unroll the groups or single inputs into vectors of workspaces
const auto &ads = AnalysisDataService::Instance();
Expand All @@ -1173,7 +1183,7 @@ bool Algorithm::checkGroups() {
// If the property is of type WorkspaceGroup then don't unroll
if (wsGroup && !wsGroupProp) {
numGroups++;
processGroups = true;
doProcessGroups = true;
m_unrolledInputWorkspaces.emplace_back(wsGroup->getAllItems());
} else {
// Single Workspace. Treat it as a "group" with only one member
Expand All @@ -1189,7 +1199,7 @@ bool Algorithm::checkGroups() {

// No groups? Get out.
if (numGroups == 0)
return processGroups;
return doProcessGroups;

// ---- Confirm that all the groups are the same size -----
// Index of the single group
Expand Down Expand Up @@ -1226,7 +1236,7 @@ bool Algorithm::checkGroups() {
} // end for each group

// If you get here, then the groups are compatible
return processGroups;
return doProcessGroups;
}

/**
Expand Down
54 changes: 53 additions & 1 deletion Framework/API/src/AnalysisDataService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/WorkspaceGroup.h"
#include <iterator>
#include <random>
#include <sstream>

namespace Mantid::API {
Expand Down Expand Up @@ -153,8 +154,9 @@ void AnalysisDataServiceImpl::rename(const std::string &oldName, const std::stri
* Overridden remove member to delete its name held by the workspace itself.
* It is important to do if the workspace isn't deleted after removal.
* @param name The name of a workspace to remove.
* @return The workspace being removed from the ADS
*/
void AnalysisDataServiceImpl::remove(const std::string &name) {
Workspace_sptr AnalysisDataServiceImpl::remove(const std::string &name) {
Workspace_sptr ws;
try {
ws = retrieve(name);
Expand All @@ -165,8 +167,58 @@ void AnalysisDataServiceImpl::remove(const std::string &name) {
if (ws) {
ws->setName("");
}
return ws;
}

/**
* @brief random lowercase letter used for generating workspace name in
* unique_name and unique_hidden_name
* @return Random Char
*/
char AnalysisDataServiceImpl::getRandomLowercaseLetter() {
static const std::string alphabet = "abcdefghijklmnopqrstuvwxyz";
static std::random_device rd;
static std::mt19937 gen(rd());
static std::uniform_int_distribution<> dis(0, int(alphabet.size() - 1));
return alphabet[dis(gen)];
}

/**
* @brief generate a unique name that will not collide with any other workspace name
* @param n Size of the sequence (must be a positive number)
* @param prefix String to prefix the random name
* @param suffix String to suffix the random name
* @return String (prefix + n*random char + suffix)
*/
const std::string AnalysisDataServiceImpl::uniqueName(const int n, const std::string &prefix,
const std::string &suffix) {
if (n <= 0) {
throw std::invalid_argument("n must be a positive number");
}
auto randomNameGenerator = [n]() {
std::string name;
for (int i = 0; i < n; ++i) {
name += getRandomLowercaseLetter();
}
return name;
};

// limit of (n * 10 * size of alphabet) to avoid infinite loop in case we can't find name that doesn't collide
for (int i = 0; i < (n * 260); i++) {
std::string wsName = prefix + randomNameGenerator() + suffix;
if (!doesExist(wsName)) {
return wsName;
}
}
throw std::runtime_error("Unable to generate unique workspace of length " + std::to_string(n));
}

/**
* @brief generate a unique hidden name to be used as a temporary workspace
* @return String ("__" + 9*random char)
*/
const std::string AnalysisDataServiceImpl::uniqueHiddenName() { return AnalysisDataServiceImpl::uniqueName(9, "__"); }

/**
* @brief Given a list of names retrieve the corresponding workspace handles
* @param names A list of names of workspaces, if any does not exist then
Expand Down
Loading

0 comments on commit ce987c9

Please sign in to comment.