Search in sources :

Example 1 with ProcessingRequest

use of org.eclipse.scanning.api.event.scan.ProcessingRequest 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 2 with ProcessingRequest

use of org.eclipse.scanning.api.event.scan.ProcessingRequest in project gda-core by openGDA.

the class SavuProcessingRequestHandlerTest method testSavuProcessingRequest.

@Test
public void testSavuProcessingRequest() throws Exception {
    ProcessingRequestHandler handler = new SavuProcessingRequestHandler();
    URL file1 = new URL("file:/lev1/lev2");
    URL file2 = new URL("file:/lev3/lev4");
    List<URL> paths = new ArrayList<>();
    paths.add(file1);
    paths.add(file2);
    SavuProcessingRequest request = new SavuProcessingRequest.Builder().withValue(paths).build();
    ScanRequest scanRequest = new ScanRequest();
    scanRequest.setProcessingRequest(new ProcessingRequest());
    scanRequest.getProcessingRequest().setRequest(new HashMap<>());
    handler.handle(request, scanRequest);
    Collection<Object> translated = scanRequest.getProcessingRequest().getRequest().get(request.getKey());
    Assert.assertEquals(2, translated.size());
    Assert.assertTrue(translated.contains(file1.getPath()));
    Assert.assertTrue(translated.contains(file2.getPath()));
}
Also used : ScanRequest(org.eclipse.scanning.api.event.scan.ScanRequest) ArrayList(java.util.ArrayList) SavuProcessingRequest(uk.ac.gda.api.acquisition.configuration.processing.SavuProcessingRequest) ProcessingRequest(org.eclipse.scanning.api.event.scan.ProcessingRequest) SavuProcessingRequest(uk.ac.gda.api.acquisition.configuration.processing.SavuProcessingRequest) URL(java.net.URL) Test(org.junit.Test)

Example 3 with ProcessingRequest

use of org.eclipse.scanning.api.event.scan.ProcessingRequest in project gda-core by openGDA.

the class ScanRequestConverterTest method testProcessingRequestIncludedCorrectly.

@Test
public void testProcessingRequestIncludedCorrectly() throws Exception {
    ConfigWrapper wrapper = new ConfigWrapper();
    String appName = "test";
    String pathToConfig = "/path/to/config";
    wrapper.setAppName(appName);
    wrapper.setPathToConfig(pathToConfig);
    wrapper.setActive(true);
    mappingBean.addProcessingRequest(wrapper);
    // Act - convert mapping bean to scan request
    final ScanRequest scanRequest = scanRequestConverter.convertToScanRequest(mappingBean);
    // Assert
    ProcessingRequest processingRequest = scanRequest.getProcessingRequest();
    testProcessingRequest(appName, pathToConfig, processingRequest);
    // Act again - merge scan request into new mapping bean
    scanRequestConverter.mergeIntoMappingBean(scanRequest, newMappingBean);
    // Assert again - check the new mapping bean is the same as the old one
    ProcessingRequest r = new ProcessingRequest();
    r.setRequest(newMappingBean.getProcessingRequest());
    testProcessingRequest(appName, pathToConfig, r);
}
Also used : ScanRequest(org.eclipse.scanning.api.event.scan.ScanRequest) ConfigWrapper(uk.ac.diamond.daq.mapping.api.ConfigWrapper) ProcessingRequest(org.eclipse.scanning.api.event.scan.ProcessingRequest) Test(org.junit.Test)

Example 4 with ProcessingRequest

use of org.eclipse.scanning.api.event.scan.ProcessingRequest in project gda-core by openGDA.

the class ApplyNexusTemplateTest method testApplyNexusTemplateRequest.

@Test
public void testApplyNexusTemplateRequest() throws Exception {
    ProcessingRequestHandler handler = new ApplyNexusTemplateHandler();
    List<URL> paths = new ArrayList<>();
    paths.add(file1);
    paths.add(file2);
    ApplyNexusTemplatesRequest request = new ApplyNexusTemplatesRequest.Builder().withValue(paths).build();
    ScanRequest scanRequest = new ScanRequest();
    scanRequest.setProcessingRequest(new ProcessingRequest());
    scanRequest.getProcessingRequest().setRequest(new HashMap<>());
    handler.handle(request, scanRequest);
    Set<String> translated = scanRequest.getTemplateFilePaths();
    Assert.assertEquals(2, translated.size());
    Assert.assertTrue(translated.contains(file1.getPath()));
    Assert.assertTrue(translated.contains(file2.getPath()));
}
Also used : ScanRequest(org.eclipse.scanning.api.event.scan.ScanRequest) ArrayList(java.util.ArrayList) ProcessingRequest(org.eclipse.scanning.api.event.scan.ProcessingRequest) ApplyNexusTemplatesRequest(uk.ac.gda.api.acquisition.configuration.processing.ApplyNexusTemplatesRequest) URL(java.net.URL) Test(org.junit.Test)

Example 5 with ProcessingRequest

use of org.eclipse.scanning.api.event.scan.ProcessingRequest in project gda-core by openGDA.

the class ProcessingRequestHandlerServiceTest method testUnknownRequest.

@Test
public void testUnknownRequest() {
    ProcessingRequestPair<Double> customPair = new ProcessingRequestPair<Double>() {

        @Override
        public String getKey() {
            return "WeightedProcess";
        }

        @Override
        public List<Double> getValue() {
            List<Double> weights = new ArrayList<>();
            weights.add(1.2);
            weights.add(3.7);
            return weights;
        }
    };
    ScanRequest scanRequest = new ScanRequest();
    scanRequest.setProcessingRequest(new ProcessingRequest());
    scanRequest.getProcessingRequest().setRequest(new HashMap<>());
    service.handle(customPair, scanRequest);
    Assert.assertEquals(0, scanRequest.getProcessingRequest().getRequest().size());
}
Also used : ProcessingRequestPair(uk.ac.gda.api.acquisition.configuration.processing.ProcessingRequestPair) ScanRequest(org.eclipse.scanning.api.event.scan.ScanRequest) ArrayList(java.util.ArrayList) ProcessingRequest(org.eclipse.scanning.api.event.scan.ProcessingRequest) Test(org.junit.Test)

Aggregations

ProcessingRequest (org.eclipse.scanning.api.event.scan.ProcessingRequest)6 ScanRequest (org.eclipse.scanning.api.event.scan.ScanRequest)6 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 URL (java.net.URL)2 File (java.io.File)1 Collection (java.util.Collection)1 TreeSet (java.util.TreeSet)1 Collectors.toCollection (java.util.stream.Collectors.toCollection)1 IDetectorModel (org.eclipse.scanning.api.device.models.IDetectorModel)1 MapPosition (org.eclipse.scanning.api.points.MapPosition)1 CompoundModel (org.eclipse.scanning.api.points.models.CompoundModel)1 IMapPathModel (org.eclipse.scanning.api.points.models.IMapPathModel)1 IScanPointGeneratorModel (org.eclipse.scanning.api.points.models.IScanPointGeneratorModel)1 ScanRegion (org.eclipse.scanning.api.points.models.ScanRegion)1 ScanningException (org.eclipse.scanning.api.scan.ScanningException)1 ConfigWrapper (uk.ac.diamond.daq.mapping.api.ConfigWrapper)1 IMappingScanRegion (uk.ac.diamond.daq.mapping.api.IMappingScanRegion)1 IScanModelWrapper (uk.ac.diamond.daq.mapping.api.IScanModelWrapper)1 IScriptFiles (uk.ac.diamond.daq.mapping.api.IScriptFiles)1