Search in sources :

Example 1 with IPosition

use of org.eclipse.scanning.api.points.IPosition 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 2 with IPosition

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

the class ScanRequestConverterTest method testBeamlineConfigurationIncludedCorrectly.

@Test
public void testBeamlineConfigurationIncludedCorrectly() throws Exception {
    // Arrange
    final Map<String, Object> beamlineConfiguration = new HashMap<>();
    beamlineConfiguration.put("energy", 12345.67);
    beamlineConfiguration.put("attenuator_pos", "Gap");
    beamlineConfiguration.put("kb_mirror_pos", 7.0);
    mappingBean.setBeamlineConfiguration(beamlineConfiguration);
    // Act - convert mapping bean to scan request
    final ScanRequest scanRequest = scanRequestConverter.convertToScanRequest(mappingBean);
    // Assert
    final IPosition startPos = scanRequest.getStartPosition();
    assertThat(startPos.getNames().size(), is(equalTo(beamlineConfiguration.size())));
    for (String scannableName : beamlineConfiguration.keySet()) {
        assertThat(startPos.get(scannableName), is(equalTo(beamlineConfiguration.get(scannableName))));
    }
    // Act again - convert scan request back to a mapping bean
    scanRequestConverter.mergeIntoMappingBean(scanRequest, newMappingBean);
    // Assert - the new beamline configuration should be equal to the original one
    final Map<String, Object> newBeamlineConfiguration = newMappingBean.getBeamlineConfiguration();
    assertThat(newBeamlineConfiguration, is(notNullValue()));
    assertThat(newBeamlineConfiguration, is(equalTo(beamlineConfiguration)));
}
Also used : ScanRequest(org.eclipse.scanning.api.event.scan.ScanRequest) IPosition(org.eclipse.scanning.api.points.IPosition) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 3 with IPosition

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

the class PointGeneratorPathInfoCalculator method calculatePathInfo.

@Override
public PathInfo calculatePathInfo(PathInfoRequest request) throws PathInfoCalculationException {
    IScanPointGeneratorModel scanPathModel = request.getScanPathModel();
    try {
        // Invokes ScanPointGenerator to create the points
        IPointGenerator<CompoundModel> pointGenerator = getPointGeneratorService().createGenerator(scanPathModel, request.getScanRegion());
        // Initialise with sensible starting values
        int pointCount = 0;
        double smallestXStep = Double.MAX_VALUE;
        double smallestYStep = Double.MAX_VALUE;
        double smallestAbsStep = Double.MAX_VALUE;
        List<Double> xCoordinates = new ArrayList<>();
        List<Double> yCoordinates = new ArrayList<>();
        double lastX = Double.NaN;
        double lastY = Double.NaN;
        String xAxisName = getXAxisName(scanPathModel);
        String yAxisName = getYAxisName(scanPathModel);
        // - The smallest distance between two consecutive positions in 2D space
        for (IPosition point : pointGenerator) {
            pointCount++;
            if (pointCount > 1) {
                // Updates the smallest distance tracking variables if the distance
                // between this point and the last is smaller than the smallest
                // distance we've seen so far. Do this for x, y and 2D space.
                double thisXStep = Math.abs(point.getValue(xAxisName) - lastX);
                double thisYStep = Math.abs(point.getValue(yAxisName) - lastY);
                double thisAbsStep = Math.sqrt(Math.pow(thisXStep, 2) + Math.pow(thisYStep, 2));
                if (thisXStep > 0) {
                    smallestXStep = Math.min(smallestXStep, thisXStep);
                }
                if (thisYStep > 0) {
                    smallestYStep = Math.min(smallestYStep, thisYStep);
                }
                smallestAbsStep = Math.min(smallestAbsStep, thisAbsStep);
            }
            // Ensures no more than MAX_POINTS_IN_ROI points are inserted into
            // the PathInfo. Still need to iterate through the rest of the points
            // to accurately calculate step sizes and number of points.
            lastX = point.getValue(xAxisName);
            lastY = point.getValue(yAxisName);
            if (xCoordinates.size() < request.getMaxPoints()) {
                xCoordinates.add(Double.valueOf(lastX));
                yCoordinates.add(Double.valueOf(lastY));
            }
        }
        // outerAxisMultiplier is the product of the number of points
        // in each outer axis. It defaults to 1 if there are no outer axes.
        // It is multiplied by the number of points in the mapping scan
        // e.g. if the mapping scan in x and y has 25 points and is
        // also projected back through 10 points in z then there are
        // 250 points in total.
        int outerAxisMultiplier = calculateAllOuterPoints(request.getOuterScannables());
        int totalPoints = outerAxisMultiplier * pointCount;
        return PathInfo.builder().withSourceId(request.getSourceId()).withInnerPointCount(pointCount).withTotalPointCount(totalPoints).withSmallestXStep(smallestXStep).withSmallestYStep(smallestYStep).withSmallestAbsStep(smallestAbsStep).withxCoordinateList(xCoordinates).withyCoordinateList(yCoordinates).build();
    } catch (GeneratorException e) {
        throw new PathInfoCalculationException(e);
    }
}
Also used : IPosition(org.eclipse.scanning.api.points.IPosition) CompoundModel(org.eclipse.scanning.api.points.models.CompoundModel) PathInfoCalculationException(uk.ac.diamond.daq.mapping.api.PathInfoCalculationException) ArrayList(java.util.ArrayList) GeneratorException(org.eclipse.scanning.api.points.GeneratorException) IScanPointGeneratorModel(org.eclipse.scanning.api.points.models.IScanPointGeneratorModel)

Example 4 with IPosition

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

the class ScannableNexusWrapperTest method testSetPosition_withScanPosition.

@Test
public void testSetPosition_withScanPosition() throws Exception {
    // Note: nexus writing tested separately by ScannableNexusWrapperScanTest
    // Arrange
    final double newPosition = 3.8;
    final int scanIndex = 38;
    IPositionListener posListener = mock(IPositionListener.class);
    ((IPositionListenable) scannable).addPositionListener(posListener);
    final IPosition scanPosition = new Scalar<Double>("sax", scanIndex, newPosition);
    assertThat(scannable.getPosition(), is(equalTo(3.7)));
    // Act
    scannable.setPosition(newPosition, scanPosition);
    // Assert
    assertThat(scannable.getPosition(), is(equalTo(newPosition)));
    ((IPositionListenable) scannable).removePositionListener(posListener);
    ArgumentCaptor<PositionEvent> captor = ArgumentCaptor.forClass(PositionEvent.class);
    InOrder order = inOrder(posListener);
    order.verify(posListener).positionWillPerform(captor.capture());
    order.verify(posListener).positionChanged(captor.capture());
    order.verify(posListener).positionPerformed(captor.capture());
    order.verifyNoMoreInteractions();
    List<PositionEvent> posEvents = captor.getAllValues();
    assertThat(posEvents, hasSize(3));
    int i = 0;
    for (PositionEvent posEvent : posEvents) {
        // willPerform event doesn't have level set
        assertThat(posEvent.getLevel(), is(i == 0 ? 0 : 5));
        IPosition position = posEvent.getPosition();
        assertThat(position.getNames(), contains(scannable.getName()));
        // index not set for positionChanged
        assertThat(position.getIndex("sax"), is(i == 1 ? -1 : 38));
        assertThat(position.getValue(scannable.getName()), is(equalTo(Double.valueOf(newPosition))));
        i++;
    }
}
Also used : IPosition(org.eclipse.scanning.api.points.IPosition) InOrder(org.mockito.InOrder) IPositionListenable(org.eclipse.scanning.api.scan.event.IPositionListenable) IPositionListener(org.eclipse.scanning.api.scan.event.IPositionListener) Scalar(org.eclipse.scanning.api.points.Scalar) PositionEvent(org.eclipse.scanning.api.scan.PositionEvent) Test(org.junit.Test)

Example 5 with IPosition

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

the class ScannableNexusWrapperTest method testSetPosition.

@Test
public void testSetPosition() throws Exception {
    // Arrange
    final double newPosition = 8.3;
    IPositionListener posListener = mock(IPositionListener.class);
    ((IPositionListenable) scannable).addPositionListener(posListener);
    assertThat(scannable.getPosition(), is(equalTo(3.7)));
    // Act
    scannable.setPosition(newPosition);
    // Assert
    assertThat(scannable.getPosition(), is(equalTo(newPosition)));
    ((IPositionListenable) scannable).removePositionListener(posListener);
    ArgumentCaptor<PositionEvent> captor = ArgumentCaptor.forClass(PositionEvent.class);
    InOrder order = inOrder(posListener);
    order.verify(posListener).positionWillPerform(captor.capture());
    order.verify(posListener).positionChanged(captor.capture());
    order.verify(posListener).positionPerformed(captor.capture());
    order.verifyNoMoreInteractions();
    List<PositionEvent> posEvents = captor.getAllValues();
    assertThat(posEvents, hasSize(3));
    int i = 0;
    for (PositionEvent posEvent : posEvents) {
        // willPerform event doesn't have level set
        assertThat(posEvent.getLevel(), is(i == 0 ? 0 : 5));
        IPosition position = posEvent.getPosition();
        assertThat(position.getNames(), contains(scannable.getName()));
        assertThat(position.getValue(scannable.getName()), is(equalTo(Double.valueOf(newPosition))));
        i++;
    }
}
Also used : IPosition(org.eclipse.scanning.api.points.IPosition) InOrder(org.mockito.InOrder) IPositionListenable(org.eclipse.scanning.api.scan.event.IPositionListenable) IPositionListener(org.eclipse.scanning.api.scan.event.IPositionListener) PositionEvent(org.eclipse.scanning.api.scan.PositionEvent) Test(org.junit.Test)

Aggregations

IPosition (org.eclipse.scanning.api.points.IPosition)10 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 Scalar (org.eclipse.scanning.api.points.Scalar)3 IPositionListenable (org.eclipse.scanning.api.scan.event.IPositionListenable)3 IPositionListener (org.eclipse.scanning.api.scan.event.IPositionListener)3 Scannable (gda.device.Scannable)2 HashMap (java.util.HashMap)2 IScannable (org.eclipse.scanning.api.IScannable)2 ScanRequest (org.eclipse.scanning.api.event.scan.ScanRequest)2 CompoundModel (org.eclipse.scanning.api.points.models.CompoundModel)2 PositionEvent (org.eclipse.scanning.api.scan.PositionEvent)2 InOrder (org.mockito.InOrder)2 DeviceException (gda.device.DeviceException)1 ScannableMotion (gda.device.ScannableMotion)1 DummyMultiFieldUnitsScannable (gda.device.scannable.DummyMultiFieldUnitsScannable)1 DummyScannable (gda.device.scannable.DummyScannable)1 DummyUnitsScannable (gda.device.scannable.DummyUnitsScannable)1 Map (java.util.Map)1 Attribute (org.eclipse.dawnsci.analysis.api.tree.Attribute)1