Search in sources :

Example 1 with ScanRegion

use of org.eclipse.scanning.api.points.models.ScanRegion in project gda-core by openGDA.

the class FocusScanConverterTest method testConvertFocusScanBean.

@Test
public void testConvertFocusScanBean() {
    final ScanRequest scanRequest = focusScanConverter.convertToScanRequest(focusScanBean);
    // test compound model
    final CompoundModel compoundModel = scanRequest.getCompoundModel();
    assertNotNull(compoundModel);
    final List<IScanPointGeneratorModel> models = compoundModel.getModels();
    assertEquals(2, models.size());
    // test outer model - the focus model
    assertThat(models.get(0), is(instanceOf(AxialStepModel.class)));
    final AxialStepModel focusModel = (AxialStepModel) models.get(0);
    assertEquals(1, focusModel.getScannableNames().size());
    assertEquals(focusScanBean.getFocusScannableName(), focusModel.getScannableNames().get(0));
    assertEquals(focusScanBean.getFocusCentre() - focusScanBean.getFocusRange() + focusModel.getStep() / 2, focusModel.getStart(), 1e-15);
    assertEquals(focusScanBean.getFocusCentre() + focusScanBean.getFocusRange() - focusModel.getStep() / 2 + focusModel.getStep() / 100, focusModel.getStop(), 1e-15);
    assertEquals(focusScanBean.getFocusRange() * 2 / focusScanBean.getNumberOfFocusSteps(), focusModel.getStep(), 1e-15);
    // test inner model - the line model
    assertThat(models.get(1), is(instanceOf(TwoAxisLinePointsModel.class)));
    final TwoAxisLinePointsModel lineModel = (TwoAxisLinePointsModel) models.get(1);
    assertEquals(2, lineModel.getScannableNames().size());
    assertEquals(mappingStageInfo.getPlotYAxisName(), lineModel.getScannableNames().get(1));
    assertEquals(mappingStageInfo.getPlotXAxisName(), lineModel.getScannableNames().get(0));
    assertEquals(focusScanBean.getNumberOfLinePoints(), lineModel.getPoints());
    final BoundingLine boundingLine = lineModel.getBoundingLine();
    assertNotNull(boundingLine);
    final LinearROI expectedRegion = (LinearROI) focusScanBean.getLineRegion().toROI();
    assertEquals(expectedRegion.getPointX(), boundingLine.getxStart(), 1e-15);
    assertEquals(expectedRegion.getPointY(), boundingLine.getyStart(), 1e-15);
    assertEquals(expectedRegion.getAngle(), boundingLine.getAngle(), 1e-15);
    assertEquals(expectedRegion.getLength(), boundingLine.getLength(), 1e-15);
    // test scan regions
    assertEquals(1, compoundModel.getRegions().size());
    final ScanRegion scanRegion = compoundModel.getRegions().iterator().next();
    assertEquals(2, scanRegion.getScannables().size());
    assertEquals(mappingStageInfo.getPlotYAxisName(), scanRegion.getScannables().get(1));
    assertEquals(mappingStageInfo.getPlotXAxisName(), scanRegion.getScannables().get(0));
    assertThat(scanRegion.getRoi(), is(instanceOf(LinearROI.class)));
    assertEquals(expectedRegion, scanRegion.getRoi());
    // test detectors
    final Map<String, IDetectorModel> detectors = scanRequest.getDetectors();
    assertNotNull(detectors);
    assertEquals(1, detectors.size());
    assertTrue(detectors.containsKey("mandelbrot"));
    assertThat(detectors.get("mandelbrot"), is(instanceOf(MandelbrotModel.class)));
    // test that the remaining fields have not been set
    assertThat(scanRequest.getMonitorNamesPerPoint(), is(empty()));
    assertThat(scanRequest.getMonitorNamesPerScan(), is(empty()));
    assertThat(scanRequest.getScanMetadata(), is(nullValue()));
    assertThat(scanRequest.getAfterScript(), is(nullValue()));
    assertThat(scanRequest.getBeforeScript(), is(nullValue()));
    assertThat(scanRequest.isAlwaysRunAfterScript(), is(false));
    assertThat(scanRequest.getStartPosition(), is(nullValue()));
    assertThat(scanRequest.getEndPosition(), is(nullValue()));
    assertThat(scanRequest.getFilePath(), is(nullValue()));
}
Also used : ScanRequest(org.eclipse.scanning.api.event.scan.ScanRequest) ScanRegion(org.eclipse.scanning.api.points.models.ScanRegion) IDetectorModel(org.eclipse.scanning.api.device.models.IDetectorModel) TwoAxisLinePointsModel(org.eclipse.scanning.api.points.models.TwoAxisLinePointsModel) CompoundModel(org.eclipse.scanning.api.points.models.CompoundModel) LinearROI(org.eclipse.dawnsci.analysis.dataset.roi.LinearROI) AxialStepModel(org.eclipse.scanning.api.points.models.AxialStepModel) IScanPointGeneratorModel(org.eclipse.scanning.api.points.models.IScanPointGeneratorModel) BoundingLine(org.eclipse.scanning.api.points.models.BoundingLine) Test(org.junit.Test)

Example 2 with ScanRegion

use of org.eclipse.scanning.api.points.models.ScanRegion in project gda-core by openGDA.

the class ScanRequestConverterTest method testRoiAxisNamesAreSet.

@Test
public void testRoiAxisNamesAreSet() throws Exception {
    IPointGeneratorService pointGeneratorService = new PointGeneratorService();
    // Act - convert mapping bean to scan request
    final ScanRequest scanRequest = scanRequestConverter.convertToScanRequest(mappingBean);
    final Collection<ScanRegion> regions = scanRequest.getCompoundModel().getRegions();
    // Assert - ensure only one region
    assertThat(regions.size(), is(equalTo(1)));
    for (final ScanRegion scanRegion : regions) {
        final List<String> scannables = scanRegion.getScannables();
        final RectangularROI roi = (RectangularROI) scanRegion.getRoi();
        /* Post DAQ-2739, this ensures if x-y axes of regions are flipped again it will be noticed either here or in the num of point
			 * [n.b. shortly before 2739, Regions were not passed to CompoundModels, then they were but reflected, which broke scanning]
			 */
        assertThat(scannables.get(0), is(equalTo(X_AXIS_NAME)));
        assertThat(scannables.get(1), is(equalTo(Y_AXIS_NAME)));
        assertEquals(X_START, roi.getPoint()[0], DIFF_LIMIT);
        assertEquals(Y_START, roi.getPoint()[1], DIFF_LIMIT);
        assertEquals(X_LENGTH, roi.getLengths()[0], DIFF_LIMIT);
        assertEquals(Y_LENGTH, roi.getLengths()[1], DIFF_LIMIT);
    }
    // Act again - convert scan request back to mapping bean
    scanRequestConverter.mergeIntoMappingBean(scanRequest, newMappingBean);
    assertEquals(mappingBean.getScanDefinition().getMappingScanRegion().getRegion(), newMappingBean.getScanDefinition().getMappingScanRegion().getRegion());
    List<IPosition> allPositions = pointGeneratorService.createCompoundGenerator(scanRequest.getCompoundModel()).createPoints();
    assertEquals(25, allPositions.size());
}
Also used : ScanRequest(org.eclipse.scanning.api.event.scan.ScanRequest) ScanRegion(org.eclipse.scanning.api.points.models.ScanRegion) IMappingScanRegion(uk.ac.diamond.daq.mapping.api.IMappingScanRegion) IPosition(org.eclipse.scanning.api.points.IPosition) IPointGeneratorService(org.eclipse.scanning.api.points.IPointGeneratorService) RectangularROI(org.eclipse.dawnsci.analysis.dataset.roi.RectangularROI) PointGeneratorService(org.eclipse.scanning.points.PointGeneratorService) IPointGeneratorService(org.eclipse.scanning.api.points.IPointGeneratorService) Test(org.junit.Test)

Example 3 with ScanRegion

use of org.eclipse.scanning.api.points.models.ScanRegion 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;
}
Also used : ScanRegion(org.eclipse.scanning.api.points.models.ScanRegion) IMappingScanRegion(uk.ac.diamond.daq.mapping.api.IMappingScanRegion) IScriptFiles(uk.ac.diamond.daq.mapping.api.IScriptFiles) TemplateFileWrapper(uk.ac.diamond.daq.mapping.api.TemplateFileWrapper) ProcessingRequest(org.eclipse.scanning.api.event.scan.ProcessingRequest) IMappingScanRegion(uk.ac.diamond.daq.mapping.api.IMappingScanRegion) ScanRequest(org.eclipse.scanning.api.event.scan.ScanRequest) IDetectorModel(org.eclipse.scanning.api.device.models.IDetectorModel) IMapPathModel(org.eclipse.scanning.api.points.models.IMapPathModel) IScanModelWrapper(uk.ac.diamond.daq.mapping.api.IScanModelWrapper) CompoundModel(org.eclipse.scanning.api.points.models.CompoundModel) TreeSet(java.util.TreeSet) Collection(java.util.Collection) Collectors.toCollection(java.util.stream.Collectors.toCollection) IScanPointGeneratorModel(org.eclipse.scanning.api.points.models.IScanPointGeneratorModel) File(java.io.File) MapPosition(org.eclipse.scanning.api.points.MapPosition)

Example 4 with ScanRegion

use of org.eclipse.scanning.api.points.models.ScanRegion 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);
}
Also used : IMappingScanRegion(uk.ac.diamond.daq.mapping.api.IMappingScanRegion) IMappingScanRegionShape(uk.ac.diamond.daq.mapping.api.IMappingScanRegionShape) ScanRegion(org.eclipse.scanning.api.points.models.ScanRegion) IMappingScanRegion(uk.ac.diamond.daq.mapping.api.IMappingScanRegion) IMapPathModel(org.eclipse.scanning.api.points.models.IMapPathModel) CompoundModel(org.eclipse.scanning.api.points.models.CompoundModel) ScriptFiles(uk.ac.diamond.daq.mapping.impl.ScriptFiles) IScriptFiles(uk.ac.diamond.daq.mapping.api.IScriptFiles) IScanDefinition(uk.ac.diamond.daq.mapping.api.IScanDefinition)

Example 5 with ScanRegion

use of org.eclipse.scanning.api.points.models.ScanRegion 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;
}
Also used : ScanRequest(org.eclipse.scanning.api.event.scan.ScanRequest) ScanRegion(org.eclipse.scanning.api.points.models.ScanRegion) IDetectorModel(org.eclipse.scanning.api.device.models.IDetectorModel) IMapPathModel(org.eclipse.scanning.api.points.models.IMapPathModel) CompoundModel(org.eclipse.scanning.api.points.models.CompoundModel) HashMap(java.util.HashMap) ILineMappingRegion(uk.ac.diamond.daq.mapping.api.ILineMappingRegion) AxialStepModel(org.eclipse.scanning.api.points.models.AxialStepModel)

Aggregations

ScanRegion (org.eclipse.scanning.api.points.models.ScanRegion)5 ScanRequest (org.eclipse.scanning.api.event.scan.ScanRequest)4 CompoundModel (org.eclipse.scanning.api.points.models.CompoundModel)4 IDetectorModel (org.eclipse.scanning.api.device.models.IDetectorModel)3 IMapPathModel (org.eclipse.scanning.api.points.models.IMapPathModel)3 IMappingScanRegion (uk.ac.diamond.daq.mapping.api.IMappingScanRegion)3 AxialStepModel (org.eclipse.scanning.api.points.models.AxialStepModel)2 IScanPointGeneratorModel (org.eclipse.scanning.api.points.models.IScanPointGeneratorModel)2 Test (org.junit.Test)2 IScriptFiles (uk.ac.diamond.daq.mapping.api.IScriptFiles)2 File (java.io.File)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 TreeSet (java.util.TreeSet)1 Collectors.toCollection (java.util.stream.Collectors.toCollection)1 LinearROI (org.eclipse.dawnsci.analysis.dataset.roi.LinearROI)1 RectangularROI (org.eclipse.dawnsci.analysis.dataset.roi.RectangularROI)1 ProcessingRequest (org.eclipse.scanning.api.event.scan.ProcessingRequest)1 IPointGeneratorService (org.eclipse.scanning.api.points.IPointGeneratorService)1 IPosition (org.eclipse.scanning.api.points.IPosition)1