use of org.eclipse.scanning.api.points.models.IMapPathModel in project gda-core by openGDA.
the class ScanRequestConverter method convertToScanRequest.
/**
* Convert an IMappingExperimentBean to a ScanRequest so that it can be run by the
* GDA9 scanning framework.
* <p>
* This will include setting the mapping scan axes with the names from the mapping axis manager.
* <p>
* This method is made <code>public</code> to allow testing.
*
* @param mappingBean
* the IMappingExperimentBean to be converted
* @return the ScanRequest
*/
public ScanRequest convertToScanRequest(IMappingExperimentBean mappingBean) {
final ScanRequest scanRequest = new ScanRequest();
final IMappingScanRegion scanRegion = mappingBean.getScanDefinition().getMappingScanRegion();
final IMapPathModel mapPath = getMapPathAndConfigureScanAxes(scanRegion);
// Build the list of models for the scan
// first get the models for any outer scannables to be included
final List<IScanPointGeneratorModel> models = mappingBean.getScanDefinition().getOuterScannables().stream().filter(IScanModelWrapper<IScanPointGeneratorModel>::isIncludeInScan).map(IScanModelWrapper<IScanPointGeneratorModel>::getModel).filter(Objects::nonNull).collect(// use array list as we're going to add an element
toCollection(ArrayList::new));
// then add the actual map path model last, it's the inner most model
models.add(mapPath);
// Convert the list of models into a compound model
final CompoundModel compoundModel = new CompoundModel(models);
// Add the ROI for the mapping region
final ScanRegion region = new ScanRegion(scanRegion.getRegion().toROI(), mapPath.getxAxisName(), mapPath.getyAxisName());
// Convert to a List of ScanRegion<IROI> containing one item to avoid unsafe varargs warning
compoundModel.setRegions(Arrays.asList(region));
// Set the model on the scan request
scanRequest.setCompoundModel(compoundModel);
// set the scan start position (scannables not in the scan that are set to a certain value before the scan starts)
final Map<String, Object> beamlineConfiguration = mappingBean.getBeamlineConfiguration();
if (beamlineConfiguration != null) {
scanRequest.setStartPosition(new MapPosition(beamlineConfiguration));
}
// add the required detectors to the scan
for (IScanModelWrapper<IDetectorModel> detectorWrapper : mappingBean.getDetectorParameters()) {
if (detectorWrapper.isIncludeInScan()) {
IDetectorModel detectorModel = detectorWrapper.getModel();
scanRequest.putDetector(detectorModel.getName(), detectorModel);
}
}
// set the per-scan and per-point monitors according to the mapping bean
configureMonitors(mappingBean, scanRequest);
// set the scripts to run before and after the scan, if any
if (mappingBean.getScriptFiles() != null) {
final IScriptFiles scriptFiles = mappingBean.getScriptFiles();
scanRequest.setBeforeScript(createScriptRequest(scriptFiles.getBeforeScanScript()));
scanRequest.setAfterScript(createScriptRequest(scriptFiles.getAfterScanScript()));
scanRequest.setAlwaysRunAfterScript(scriptFiles.isAlwaysRunAfterScript());
}
// add the sample metadata
if (mappingBean.getSampleMetadata() != null) {
setSampleMetadata(mappingBean, scanRequest);
}
// Add required processing
Map<String, Collection<Object>> processingRequest = mappingBean.getProcessingRequest();
ProcessingRequest r = new ProcessingRequest();
r.setRequest(processingRequest);
scanRequest.setProcessingRequest(r);
// Add template files
final List<TemplateFileWrapper> templateFiles = mappingBean.getTemplateFiles();
if (templateFiles != null && !templateFiles.isEmpty()) {
final Set<String> existingTemplateFilePaths = scanRequest.getTemplateFilePaths();
final Set<String> allTemplateFilePaths = new TreeSet<>();
Optional.ofNullable(existingTemplateFilePaths).ifPresent(allTemplateFilePaths::addAll);
templateFiles.stream().filter(TemplateFileWrapper::isActive).forEach(fp -> allTemplateFilePaths.add(fp.getFilePath()));
scanRequest.setTemplateFilePaths(allTemplateFilePaths);
}
// Add alternative output directory if selected and valid
if (mappingBean.isUseAlternativeDirectory()) {
final String outputDirString = mappingBean.getAlternativeDirectory();
final File outputDir = new File(outputDirString);
if (outputDir.isDirectory()) {
scanRequest.setFilePath(outputDirString);
} else {
logger.warn("Cannot write output to {}: it is not a directory", outputDirString);
}
}
return scanRequest;
}
use of org.eclipse.scanning.api.points.models.IMapPathModel in project gda-core by openGDA.
the class ScanRequestConverter method getMapPathAndConfigureScanAxes.
private IMapPathModel getMapPathAndConfigureScanAxes(IMappingScanRegion scanRegion) {
// check the scan path is an IMapPathModel
if (!(scanRegion.getScanPath() instanceof IMapPathModel)) {
final String message = "Could not set fast and slow axis. The scan path is not an instance of IMapPathModel.";
logger.error(message);
throw new IllegalArgumentException(message);
}
// get the fast and slow axis of the scan.
final IMapPathModel mapPath = (IMapPathModel) scanRegion.getScanPath();
if (mappingStageInfo != null) {
// If the mapping stage is set, use these axis, and update the default map path with them
String xAxisName = mappingStageInfo.getPlotXAxisName();
String yAxisName = mappingStageInfo.getPlotYAxisName();
mapPath.setxAxisName(xAxisName);
mapPath.setyAxisName(yAxisName);
ScannableMotionUnits xAxis = Finder.find(xAxisName);
Optional.ofNullable(xAxis).ifPresent(axis -> mapPath.setxAxisUnits(axis.getHardwareUnitString()));
ScannableMotionUnits yAxis = Finder.find(yAxisName);
Optional.ofNullable(yAxis).ifPresent(axis -> mapPath.setyAxisUnits(axis.getHardwareUnitString()));
} else {
// Otherwise we use the default axis in the map path model
logger.warn("No mapping axis manager is set - the scan request will use default axis names and uni!");
}
return mapPath;
}
use of org.eclipse.scanning.api.points.models.IMapPathModel in project gda-core by openGDA.
the class ScanRequestConverter method checkMapModelAndUpdateMappingStage.
private IMapPathModel checkMapModelAndUpdateMappingStage(final CompoundModel compoundModel) {
final List<IScanPointGeneratorModel> models = compoundModel.getModels();
// check that the inner most model is an IMapPathModel, i.e. for a mapping scan
final IScanPointGeneratorModel innerModelObj = models.get(models.size() - 1);
if (!(innerModelObj instanceof IMapPathModel)) {
throw new IllegalArgumentException("The inner most model is not a map model. This is not mapping scan");
}
final IMapPathModel mapPath = (IMapPathModel) innerModelObj;
// update the mapping bean with axes in the mapping path
if (mappingStageInfo != null) {
mappingStageInfo.setPlotXAxisName(mapPath.getxAxisName());
mappingStageInfo.setPlotYAxisName(mapPath.getyAxisName());
}
return mapPath;
}
use of org.eclipse.scanning.api.points.models.IMapPathModel in project gda-core by openGDA.
the class ScanRequestConverter method mergeIntoMappingBean.
/**
* Merge a scan request into an existing mapping bean so that it can be viewed
* and possibly modified in the Mapping Experiment Setup view.
* The reason for merging into an existing mapping bean is so that we don't remove
* detectors and processing steps that we not selected when this scan was run -
* when creating the scan request from the mapping bean a detector is only added if
* {@link IScanModelWrapper#isIncludeInScan()} is true. The mapping bean reconstructed
* from the scan request still needs to include this detector.
*
* @param scanRequest the {@link ScanRequest}
* @param mappingBean the {@link IMappingExperimentBean} to merge into
*/
public void mergeIntoMappingBean(ScanRequest scanRequest, IMappingExperimentBean mappingBean) {
final CompoundModel compoundModel = scanRequest.getCompoundModel();
final Collection<ScanRegion> regions = compoundModel.getRegions();
if (regions.size() != 1) {
throw new IllegalArgumentException("The scan request must have exactly one region, has " + regions.size());
}
// Check that the scannable names in the scan region are the same as in the mapping stage
final IMapPathModel mapPath = checkMapModelAndUpdateMappingStage(compoundModel);
// recreate the outer scannable wrappers from the scan request
mergeOuterScannables(compoundModel, mappingBean);
// set the scan path to the last child model of the compound model
final IScanDefinition scanDefinition = mappingBean.getScanDefinition();
final IMappingScanRegion scanRegion = scanDefinition.getMappingScanRegion();
scanRegion.setScanPath(mapPath);
// convert the ROI to a mapping scan region shape
final ScanRegion region = regions.iterator().next();
final IMappingScanRegionShape shape = convertROItoRegionShape(region.getRoi());
scanRegion.setRegion(shape);
// recreate the beamline configuration from the scan start position
if (scanRequest.getStartPosition() != null) {
mappingBean.setBeamlineConfiguration(new LinkedHashMap<>(scanRequest.getStartPosition().getValues()));
}
// recreate the detector models and processing steps (included in the same map of detectors in the scan request)
mergeDetectorAndProcessing(scanRequest, mappingBean);
// recreate the scripts to run before and after the scan, if any
if (scanRequest.getBeforeScript() != null || scanRequest.getAfterScript() != null) {
ScriptFiles scriptFiles = new ScriptFiles();
if (scanRequest.getBeforeScript() != null) {
scriptFiles.setBeforeScanScript(scanRequest.getBeforeScript().getFile());
}
if (scanRequest.getAfterScript() != null) {
scriptFiles.setAfterScanScript(scanRequest.getAfterScript().getFile());
}
scriptFiles.setAlwaysRunAfterScript(scanRequest.isAlwaysRunAfterScript());
mappingBean.setScriptFiles(scriptFiles);
}
// recreate the sample metadata from the metadata in the scan request
mergeSampleMetadata(scanRequest, mappingBean);
mergeTemplateFiles(scanRequest, mappingBean);
mergeProcessingRequest(scanRequest, mappingBean);
}
use of org.eclipse.scanning.api.points.models.IMapPathModel in project gda-core by openGDA.
the class FocusScanConverter method convertToScanRequest.
public ScanRequest convertToScanRequest(FocusScanBean focusScanBean) {
logger.debug("Converting focusScanBean to scan request");
final ScanRequest scanRequest = new ScanRequest();
final IMapPathModel lineModel = createLineModel(focusScanBean);
final ILineMappingRegion lineRegion = focusScanBean.getLineRegion();
final ScanRegion scanRegion = new ScanRegion(lineRegion.toROI(), lineModel.getxAxisName(), lineModel.getyAxisName());
final AxialStepModel focusModel = createFocusPathModel(focusScanBean);
final CompoundModel compoundModel = new CompoundModel(Arrays.asList(focusModel, lineModel));
compoundModel.setRegions(Arrays.asList(scanRegion));
scanRequest.setCompoundModel(compoundModel);
// add detectors
final IDetectorModel detectorModel = focusScanBean.getDetector();
final Map<String, IDetectorModel> detectorsMap = new HashMap<>();
detectorsMap.put(detectorModel.getName(), detectorModel);
scanRequest.setDetectors(detectorsMap);
return scanRequest;
}
Aggregations