예제 #1
0
파일: MaskMD.cpp 프로젝트: DanNixon/mantid
/** Execute the algorithm.
 */
void MaskMD::exec() {
  IMDWorkspace_sptr ws = getProperty("Workspace");
  std::string dimensions_string = getPropertyValue("Dimensions");
  std::vector<double> extents = getProperty("Extents");

  // Dimension names may contain brackets with commas (i.e. [H,0,0])
  // so getProperty would return an incorrect vector of names;
  // instead get the string and parse it here
  std::vector<std::string> dimensions = parseDimensionNames(dimensions_string);
  // Report what dimension names were found
  g_log.debug() << "Dimension names parsed as: \n";
  for (const auto &name : dimensions) {
    g_log.debug() << name << '\n';
  }

  size_t nDims = ws->getNumDims();
  size_t nDimensionIds = dimensions.size();

  size_t nGroups = nDimensionIds / nDims;

  bool bClearExistingMasks = getProperty("ClearExistingMasks");
  if (bClearExistingMasks) {
    ws->clearMDMasking();
  }
  this->interruption_point();
  this->progress(0.0);

  // Explicitly cast nGroups and group to double to avoid compiler warnings
  // loss of precision does not matter as we are only using the result
  // for reporting algorithm progress
  const double nGroups_double = static_cast<double>(nGroups);
  // Loop over all groups
  for (size_t group = 0; group < nGroups; ++group) {
    std::vector<InputArgument> arguments(nDims);

    // Loop over all arguments within the group. and construct InputArguments
    // for sorting.
    for (size_t i = 0; i < nDims; ++i) {
      size_t index = i + (group * nDims);
      InputArgument &arg = arguments[i];

      // Try to get the index of the dimension in the workspace.
      arg.index = tryFetchDimensionIndex(ws, dimensions[index]);

      arg.min = extents[index * 2];
      arg.max = extents[(index * 2) + 1];
    }

    // Sort all the inputs by the dimension index. Without this it will not be
    // possible to construct the MDImplicit function property.
    LessThanIndex comparator;
    std::sort(arguments.begin(), arguments.end(), comparator);

    // Create inputs for a box implicit function
    VMD mins(nDims);
    VMD maxs(nDims);
    for (size_t i = 0; i < nDims; ++i) {
      mins[i] = float(arguments[i].min);
      maxs[i] = float(arguments[i].max);
    }

    // Add new masking.
    ws->setMDMasking(new MDBoxImplicitFunction(mins, maxs));
    this->interruption_point();
    double group_double = static_cast<double>(group);
    this->progress(group_double / nGroups_double);
  }
  this->progress(1.0); // Ensure algorithm progress is reported as complete
}
예제 #2
0
/** Execute the algorithm.
 */
void MaskMD::exec() {
  IMDWorkspace_sptr ws = getProperty("Workspace");
  std::vector<std::string> dimensions = getProperty("Dimensions");
  std::vector<double> extents = getProperty("Extents");

  size_t nDims = ws->getNumDims();
  size_t nDimensionIds = dimensions.size();

  std::stringstream messageStream;

  // Check cardinality on names/ids
  if (nDimensionIds % nDims != 0) {
    messageStream << "Number of dimension ids/names must be n * " << nDims;
    this->g_log.error(messageStream.str());
    throw std::invalid_argument(messageStream.str());
  }

  // Check cardinality on extents
  if (extents.size() != (2 * dimensions.size())) {
    messageStream << "Number of extents must be " << 2 * dimensions.size();
    this->g_log.error(messageStream.str());
    throw std::invalid_argument(messageStream.str());
  }

  // Check extent value provided.
  for (size_t i = 0; i < nDimensionIds; ++i) {
    double min = extents[i * 2];
    double max = extents[(i * 2) + 1];
    if (min > max) {
      messageStream << "Cannot have minimum extents " << min
                    << " larger than maximum extents " << max;
      this->g_log.error(messageStream.str());
      throw std::invalid_argument(messageStream.str());
    }
  }

  size_t nGroups = nDimensionIds / nDims;

  bool bClearExistingMasks = getProperty("ClearExistingMasks");
  if (bClearExistingMasks) {
    ws->clearMDMasking();
  }

  // Loop over all groups
  for (size_t group = 0; group < nGroups; ++group) {
    std::vector<InputArgument> arguments(nDims);

    // Loop over all arguments within the group. and construct InputArguments
    // for sorting.
    for (size_t i = 0; i < nDims; ++i) {
      size_t index = i + (group * nDims);
      InputArgument &arg = arguments[i];

      // Try to get the index of the dimension in the workspace.
      arg.index = tryFetchDimensionIndex(ws, dimensions[index]);

      arg.min = extents[index * 2];
      arg.max = extents[(index * 2) + 1];
    }

    // Sort all the inputs by the dimension index. Without this it will not be
    // possible to construct the MDImplicit function propertly.
    LessThanIndex comparitor;
    std::sort(arguments.begin(), arguments.end(), comparitor);

    // Create inputs for a box implicit function
    VMD mins(nDims);
    VMD maxs(nDims);
    for (size_t i = 0; i < nDims; ++i) {
      mins[i] = float(arguments[i].min);
      maxs[i] = float(arguments[i].max);
    }

    // Add new masking.
    ws->setMDMasking(new MDBoxImplicitFunction(mins, maxs));
  }
}