Exemplo n.º 1
0
    def test_Q(self):
        ConvertWANDSCDtoQTest_out=ConvertWANDSCDtoQ('ConvertWANDSCDtoQTest_data',BinningDim0='-8.08,8.08,101',
                                                    BinningDim1='-0.88,0.88,11',BinningDim2='-8.08,8.08,101',NormaliseBy='None')

        self.assertTrue(ConvertWANDSCDtoQTest_out)

        s = ConvertWANDSCDtoQTest_out.getSignalArray()
        self.assertAlmostEqual(np.nanmax(s), 8.97233331213612)
        self.assertAlmostEqual(np.nanargmax(s), 22780)

        self.assertEquals(ConvertWANDSCDtoQTest_out.getNumDims(), 3)
        self.assertEquals(ConvertWANDSCDtoQTest_out.getNPoints(), 112211)

        d0 = ConvertWANDSCDtoQTest_out.getDimension(0)
        self.assertEquals(d0.name, 'Q_sample_x')
        self.assertEquals(d0.getNBins(), 101)
        self.assertAlmostEquals(d0.getMinimum(), -8.08, 5)
        self.assertAlmostEquals(d0.getMaximum(), 8.08, 5)

        d1 = ConvertWANDSCDtoQTest_out.getDimension(1)
        self.assertEquals(d1.name, 'Q_sample_y')
        self.assertEquals(d1.getNBins(), 11)
        self.assertAlmostEquals(d1.getMinimum(), -0.88, 5)
        self.assertAlmostEquals(d1.getMaximum(), 0.88, 5)

        d2 = ConvertWANDSCDtoQTest_out.getDimension(2)
        self.assertEquals(d2.name, 'Q_sample_z')
        self.assertEquals(d2.getNBins(), 101)
        self.assertAlmostEquals(d2.getMinimum(), -8.08, 5)
        self.assertAlmostEquals(d2.getMaximum(), 8.08, 5)

        self.assertEqual(ConvertWANDSCDtoQTest_out.getNumExperimentInfo(), 1)

        ConvertWANDSCDtoQTest_out.delete()
Exemplo n.º 2
0
    def test_Q_norm(self):
        ConvertWANDSCDtoQTest_out = ConvertWANDSCDtoQ('ConvertWANDSCDtoQTest_data',NormalisationWorkspace='ConvertWANDSCDtoQTest_norm',
                                                      BinningDim0='-8.08,8.08,101',BinningDim1='-0.88,0.88,11',BinningDim2='-8.08,8.08,101')

        s = ConvertWANDSCDtoQTest_out.getSignalArray()
        self.assertAlmostEqual(np.nanmax(s), 7.476944426780101)
        self.assertAlmostEqual(np.nanargmax(s), 22780)

        ConvertWANDSCDtoQTest_out.delete()
Exemplo n.º 3
0
    def test_Q_norm(self):
        ConvertWANDSCDtoQTest_out = ConvertWANDSCDtoQ('ConvertWANDSCDtoQTest_data',NormalisationWorkspace='ConvertWANDSCDtoQTest_norm',
                                                      BinningDim0='-8.08,8.08,101',BinningDim1='-0.88,0.88,11',BinningDim2='-8.08,8.08,101')

        s = ConvertWANDSCDtoQTest_out.getSignalArray()
        self.assertAlmostEqual(np.nanmax(s), 7.476944426780101)
        self.assertAlmostEqual(np.nanargmax(s), 22780)

        ConvertWANDSCDtoQTest_out.delete()
Exemplo n.º 4
0
    def test_HKL_norm_and_KeepTemporary(self):
        ConvertWANDSCDtoQTest_out = ConvertWANDSCDtoQ('ConvertWANDSCDtoQTest_data',NormalisationWorkspace='ConvertWANDSCDtoQTest_norm',
                                                      Frame='HKL',KeepTemporaryWorkspaces=True,BinningDim0='-8.08,8.08,101',
                                                      BinningDim1='-8.08,8.08,101',BinningDim2='-8.08,8.08,101',
                                                      Uproj='1,1,0',Vproj='1,-1,0',Wproj='0,0,1')

        self.assertTrue(ConvertWANDSCDtoQTest_out)
        self.assertTrue(mtd.doesExist('ConvertWANDSCDtoQTest_out'))
        self.assertTrue(mtd.doesExist('ConvertWANDSCDtoQTest_out_data'))
        self.assertTrue(mtd.doesExist('ConvertWANDSCDtoQTest_out_normalization'))

        s = ConvertWANDSCDtoQTest_out.getSignalArray()
        self.assertAlmostEqual(np.nanmax(s), 4.646855396509936)
        self.assertAlmostEqual(np.nanargmax(s), 443011)

        self.assertEquals(ConvertWANDSCDtoQTest_out.getNumDims(), 3)
        self.assertEquals(ConvertWANDSCDtoQTest_out.getNPoints(), 101**3)

        d0 = ConvertWANDSCDtoQTest_out.getDimension(0)
        self.assertEquals(d0.name, '[H,H,0]')
        self.assertEquals(d0.getNBins(), 101)
        self.assertAlmostEquals(d0.getMinimum(), -8.08, 5)
        self.assertAlmostEquals(d0.getMaximum(), 8.08, 5)

        d1 = ConvertWANDSCDtoQTest_out.getDimension(1)
        self.assertEquals(d1.name, '[H,-H,0]')
        self.assertEquals(d1.getNBins(), 101)
        self.assertAlmostEquals(d1.getMinimum(), -8.08, 5)
        self.assertAlmostEquals(d1.getMaximum(), 8.08, 5)

        d2 = ConvertWANDSCDtoQTest_out.getDimension(2)
        self.assertEquals(d2.name, '[0,0,L]')
        self.assertEquals(d2.getNBins(), 101)
        self.assertAlmostEquals(d2.getMinimum(), -8.08, 5)
        self.assertAlmostEquals(d2.getMaximum(), 8.08, 5)

        self.assertEqual(ConvertWANDSCDtoQTest_out.getNumExperimentInfo(), 1)

        ConvertWANDSCDtoQTest_out.delete()
Exemplo n.º 5
0
    def test_COP(self):
        ConvertWANDSCDtoQTest_out = ConvertWANDSCDtoQ(
            'ConvertWANDSCDtoQTest_data',
            BinningDim0='-8.08,8.08,101',
            BinningDim1='-1.68,1.68,21',
            BinningDim2='-8.08,8.08,101',
            NormaliseBy='None')

        ConvertWANDSCDtoQTest_cop = ConvertWANDSCDtoQ(
            'ConvertWANDSCDtoQTest_data',
            BinningDim0='-8.08,8.08,101',
            BinningDim1='-1.68,1.68,21',
            BinningDim2='-8.08,8.08,101',
            NormaliseBy='None',
            ObliquityParallaxCoefficient=1.5)

        self.assertTrue(ConvertWANDSCDtoQTest_out)
        self.assertTrue(ConvertWANDSCDtoQTest_cop)

        Test_out = ConvertWANDSCDtoQTest_out.getSignalArray().copy()
        Test_cop = ConvertWANDSCDtoQTest_cop.getSignalArray().copy()

        x, y, z = np.meshgrid(np.linspace(-8, 8, 101),
                              np.linspace(-1.6, 1.6, 21),
                              np.linspace(-8, 8, 101),
                              indexing='ij')

        Test_out_max_Qy = y[~np.isnan(Test_out)].max()
        Test_cop_max_Qy = y[~np.isnan(Test_cop)].max()

        # Test whether Qy is scaled by ObliquityParallaxCoefficient correctly
        proportion = Test_cop_max_Qy / Test_out_max_Qy

        self.assertAlmostEquals(proportion, 1.5, 5)

        ConvertWANDSCDtoQTest_out.delete()
        ConvertWANDSCDtoQTest_cop.delete()
Exemplo n.º 6
0
    def test_Q(self):
        ConvertWANDSCDtoQTest_out = ConvertWANDSCDtoQ(
            'ConvertWANDSCDtoQTest_data',
            BinningDim0='-8.08,8.08,101',
            BinningDim1='-0.88,0.88,11',
            BinningDim2='-8.08,8.08,101',
            NormaliseBy='None')

        self.assertTrue(ConvertWANDSCDtoQTest_out)

        s = ConvertWANDSCDtoQTest_out.getSignalArray()
        self.assertAlmostEqual(np.nanmax(s), 8.97233331213612)
        self.assertAlmostEqual(np.nanargmax(s), 22780)

        self.assertEqual(ConvertWANDSCDtoQTest_out.getNumDims(), 3)
        self.assertEqual(ConvertWANDSCDtoQTest_out.getNPoints(), 112211)

        d0 = ConvertWANDSCDtoQTest_out.getDimension(0)
        self.assertEqual(d0.name, 'Q_sample_x')
        self.assertEqual(d0.getNBins(), 101)
        self.assertAlmostEquals(d0.getMinimum(), -8.08, 5)
        self.assertAlmostEquals(d0.getMaximum(), 8.08, 5)

        d1 = ConvertWANDSCDtoQTest_out.getDimension(1)
        self.assertEqual(d1.name, 'Q_sample_y')
        self.assertEqual(d1.getNBins(), 11)
        self.assertAlmostEquals(d1.getMinimum(), -0.88, 5)
        self.assertAlmostEquals(d1.getMaximum(), 0.88, 5)

        d2 = ConvertWANDSCDtoQTest_out.getDimension(2)
        self.assertEqual(d2.name, 'Q_sample_z')
        self.assertEqual(d2.getNBins(), 101)
        self.assertAlmostEquals(d2.getMinimum(), -8.08, 5)
        self.assertAlmostEquals(d2.getMaximum(), 8.08, 5)

        self.assertEqual(ConvertWANDSCDtoQTest_out.getNumExperimentInfo(), 1)

        ConvertWANDSCDtoQTest_out.delete()
Exemplo n.º 7
0
    def test_HKL_norm_and_KeepTemporary(self):
        ConvertWANDSCDtoQTest_out = ConvertWANDSCDtoQ(
            'ConvertWANDSCDtoQTest_data',
            NormalisationWorkspace='ConvertWANDSCDtoQTest_norm',
            Frame='HKL',
            KeepTemporaryWorkspaces=True,
            BinningDim0='-8.08,8.08,101',
            BinningDim1='-8.08,8.08,101',
            BinningDim2='-8.08,8.08,101',
            Uproj='1,1,0',
            Vproj='1,-1,0',
            Wproj='0,0,1')

        self.assertTrue(ConvertWANDSCDtoQTest_out)
        self.assertTrue(mtd.doesExist('ConvertWANDSCDtoQTest_out'))
        self.assertTrue(mtd.doesExist('ConvertWANDSCDtoQTest_out_data'))
        self.assertTrue(
            mtd.doesExist('ConvertWANDSCDtoQTest_out_normalization'))

        s = ConvertWANDSCDtoQTest_out.getSignalArray()
        self.assertAlmostEqual(np.nanmax(s), 4.646855396509936)
        self.assertAlmostEqual(np.nanargmax(s), 443011)

        self.assertEqual(ConvertWANDSCDtoQTest_out.getNumDims(), 3)
        self.assertEqual(ConvertWANDSCDtoQTest_out.getNPoints(), 101**3)

        d0 = ConvertWANDSCDtoQTest_out.getDimension(0)
        self.assertEqual(d0.name, '[H,H,0]')
        self.assertEqual(d0.getNBins(), 101)
        self.assertAlmostEquals(d0.getMinimum(), -8.08, 5)
        self.assertAlmostEquals(d0.getMaximum(), 8.08, 5)

        d1 = ConvertWANDSCDtoQTest_out.getDimension(1)
        self.assertEqual(d1.name, '[H,-H,0]')
        self.assertEqual(d1.getNBins(), 101)
        self.assertAlmostEquals(d1.getMinimum(), -8.08, 5)
        self.assertAlmostEquals(d1.getMaximum(), 8.08, 5)

        d2 = ConvertWANDSCDtoQTest_out.getDimension(2)
        self.assertEqual(d2.name, '[0,0,L]')
        self.assertEqual(d2.getNBins(), 101)
        self.assertAlmostEquals(d2.getMinimum(), -8.08, 5)
        self.assertAlmostEquals(d2.getMaximum(), 8.08, 5)

        self.assertEqual(ConvertWANDSCDtoQTest_out.getNumExperimentInfo(), 1)

        ConvertWANDSCDtoQTest_out.delete()
Exemplo n.º 8
0
    def runTest(self):
        S = np.random.random(32 * 240 * 100)

        ConvertWANDSCDtoQTest_data = CreateMDHistoWorkspace(
            Dimensionality=3,
            Extents='0.5,32.5,0.5,240.5,0.5,100.5',
            SignalInput=S.ravel('F'),
            ErrorInput=np.sqrt(S.ravel('F')),
            NumberOfBins='32,240,100',
            Names='y,x,scanIndex',
            Units='bin,bin,number')

        ConvertWANDSCDtoQTest_dummy = CreateSingleValuedWorkspace()
        LoadInstrument(ConvertWANDSCDtoQTest_dummy,
                       InstrumentName='WAND',
                       RewriteSpectraMap=False)

        ConvertWANDSCDtoQTest_data.addExperimentInfo(
            ConvertWANDSCDtoQTest_dummy)

        log = FloatTimeSeriesProperty('s1')
        for t, v in zip(range(100), np.arange(0, 50, 0.5)):
            log.addValue(t, v)
        ConvertWANDSCDtoQTest_data.getExperimentInfo(0).run()['s1'] = log
        ConvertWANDSCDtoQTest_data.getExperimentInfo(0).run().addProperty(
            'duration', [60.] * 100, True)
        ConvertWANDSCDtoQTest_data.getExperimentInfo(0).run().addProperty(
            'monitor_count', [120000.] * 100, True)
        ConvertWANDSCDtoQTest_data.getExperimentInfo(0).run().addProperty(
            'twotheta', list(np.linspace(np.pi * 2 / 3, 0, 240).repeat(32)),
            True)
        ConvertWANDSCDtoQTest_data.getExperimentInfo(0).run().addProperty(
            'azimuthal', list(np.tile(np.linspace(-0.15, 0.15, 32), 240)),
            True)

        peaks = CreatePeaksWorkspace(NumberOfPeaks=0,
                                     OutputType='LeanElasticPeak')

        SetUB(ConvertWANDSCDtoQTest_data,
              5,
              5,
              7,
              90,
              90,
              120,
              u=[-1, 0, 1],
              v=[1, 0, 1])
        SetGoniometer(ConvertWANDSCDtoQTest_data,
                      Axis0='s1,0,1,0,1',
                      Average=False)

        CopySample(InputWorkspace=ConvertWANDSCDtoQTest_data,
                   OutputWorkspace=peaks,
                   CopyName=False,
                   CopyMaterial=False,
                   CopyEnvironment=False,
                   CopyShape=False,
                   CopyLattice=True)

        Q = ConvertWANDSCDtoQ(InputWorkspace=ConvertWANDSCDtoQTest_data,
                              UBWorkspace=peaks,
                              Wavelength=1.486,
                              Frame='HKL',
                              Uproj='1,1,0',
                              Vproj='-1,1,0',
                              BinningDim0='-6.04,6.04,151',
                              BinningDim1='-6.04,6.04,151',
                              BinningDim2='-6.04,6.04,151')

        data_norm = ConvertHFIRSCDtoMDE(ConvertWANDSCDtoQTest_data,
                                        Wavelength=1.486,
                                        MinValues='-6.04,-6.04,-6.04',
                                        MaxValues='6.04,6.04,6.04')

        HKL = ConvertQtoHKLMDHisto(data_norm,
                                   PeaksWorkspace=peaks,
                                   Uproj='1,1,0',
                                   Vproj='-1,1,0',
                                   Extents='-6.04,6.04,-6.04,6.04,-6.04,6.04',
                                   Bins='151,151,151')

        for i in range(HKL.getNumDims()):
            print(HKL.getDimension(i).getUnits(), Q.getDimension(i).getUnits())
            np.testing.assert_equal(
                HKL.getDimension(i).getUnits(),
                Q.getDimension(i).getUnits())

        hkl_data = mtd["HKL"].getSignalArray()
        Q_data = mtd["Q"].getSignalArray()

        print(np.isnan(Q_data).sum())
        print(np.isclose(hkl_data, 0).sum())

        xaxis = mtd["HKL"].getXDimension()
        yaxis = mtd["HKL"].getYDimension()
        zaxis = mtd["HKL"].getZDimension()

        x, y, z = np.meshgrid(
            np.linspace(xaxis.getMinimum(), xaxis.getMaximum(),
                        xaxis.getNBins()),
            np.linspace(yaxis.getMinimum(), yaxis.getMaximum(),
                        yaxis.getNBins()),
            np.linspace(zaxis.getMinimum(), zaxis.getMaximum(),
                        zaxis.getNBins()),
            indexing="ij",
            copy=False,
        )

        print(
            x[~np.isnan(Q_data)].mean(),
            y[~np.isnan(Q_data)].mean(),
            z[~np.isnan(Q_data)].mean(),
        )
        print(
            x[~np.isclose(hkl_data, 0)].mean(),
            y[~np.isclose(hkl_data, 0)].mean(),
            z[~np.isclose(hkl_data, 0)].mean(),
        )
        np.testing.assert_almost_equal(x[~np.isnan(Q_data)].mean(),
                                       x[~np.isclose(hkl_data, 0)].mean(),
                                       decimal=2)
        np.testing.assert_almost_equal(y[~np.isnan(Q_data)].mean(),
                                       y[~np.isclose(hkl_data, 0)].mean(),
                                       decimal=2)
        np.testing.assert_almost_equal(z[~np.isnan(Q_data)].mean(),
                                       z[~np.isclose(hkl_data, 0)].mean(),
                                       decimal=1)
Exemplo n.º 9
0
    def PyExec(self):
        load_van = not self.getProperty("VanadiumFile").isDefault
        load_files = not self.getProperty("Filename").isDefault

        output = self.getProperty("OutputType").value

        if load_files:
            datafiles = self.getProperty("Filename").value
        else:
            datafiles = list(
                map(str.strip,
                    self.getProperty("InputWorkspaces").value.split(",")))

        prog = Progress(self, 0.0, 1.0, len(datafiles) + 1)

        vanadiumfile = self.getProperty("VanadiumFile").value
        vanws = self.getProperty("VanadiumWorkspace").value
        height = self.getProperty("DetectorHeightOffset").value
        distance = self.getProperty("DetectorDistanceOffset").value

        wslist = []

        out_ws = self.getPropertyValue("OutputWorkspace")
        out_ws_name = out_ws

        if load_van:
            vanws = LoadMD(vanadiumfile, StoreInADS=False)

        has_multiple = len(datafiles) > 1

        for in_file in datafiles:
            if load_files:
                scan = LoadMD(in_file,
                              LoadHistory=False,
                              OutputWorkspace="__scan")
            else:
                scan = mtd[in_file]

            # Make sure the workspace has experiment info, otherwise SetGoniometer will add some, causing issues.
            if scan.getNumExperimentInfo() == 0:
                raise RuntimeError(
                    "No experiment info was found in '{}'".format(in_file))

            prog.report()
            self.log().information("Processing '{}'".format(in_file))

            SetGoniometer(Workspace=scan,
                          Axis0='omega,0,1,0,-1',
                          Axis1='chi,0,0,1,-1',
                          Axis2='phi,0,1,0,-1',
                          Average=False)
            # If processing multiple files, append the base name to the given output name
            if has_multiple:
                if load_files:
                    out_ws_name = out_ws + "_" + os.path.basename(
                        in_file).strip(',.nxs')
                else:
                    out_ws_name = out_ws + "_" + in_file
                wslist.append(out_ws_name)

            exp_info = scan.getExperimentInfo(0)
            self.__move_components(exp_info, height, distance)

            # Get the wavelength from experiment info if it exists, or fallback on property value
            wavelength = self.__get_wavelength(exp_info)

            # set the run number to be the same as scan number, this will be used for peaks
            if not exp_info.run().hasProperty('run_number') and exp_info.run(
            ).hasProperty('scan'):
                try:
                    exp_info.mutableRun().addProperty(
                        'run_number',
                        int(exp_info.run().getProperty('scan').value), True)
                except ValueError:
                    # scan must be a int
                    pass

            # Use ConvertHFIRSCDtoQ (and normalize van), or use ConvertWANDSCtoQ which handles normalization itself
            if output == "Q-sample events":
                norm_data = self.__normalization(scan, vanws, load_files)
                minvals = self.getProperty("MinValues").value
                maxvals = self.getProperty("MaxValues").value
                merge = self.getProperty("MergeInputs").value
                ConvertHFIRSCDtoMDE(InputWorkspace=norm_data,
                                    Wavelength=wavelength,
                                    MinValues=minvals,
                                    MaxValues=maxvals,
                                    OutputWorkspace=out_ws_name)
                DeleteWorkspace(norm_data)
            elif output == 'Q-sample histogram':
                bin0 = self.getProperty("BinningDim0").value
                bin1 = self.getProperty("BinningDim1").value
                bin2 = self.getProperty("BinningDim2").value
                # Convert to Q space and normalize with from the vanadium
                ConvertWANDSCDtoQ(
                    InputWorkspace=scan,
                    NormalisationWorkspace=vanws,
                    Frame='Q_sample',
                    Wavelength=wavelength,
                    NormaliseBy=self.getProperty("NormaliseBy").value,
                    BinningDim0=bin0,
                    BinningDim1=bin1,
                    BinningDim2=bin2,
                    OutputWorkspace=out_ws_name)
                if load_files:
                    DeleteWorkspace(scan)
            else:
                norm_data = self.__normalization(scan, vanws, load_files)
                RenameWorkspace(norm_data, OutputWorkspace=out_ws_name)

        if has_multiple:
            out_ws_name = out_ws
            if output == "Q-sample events" and merge:
                MergeMD(InputWorkspaces=wslist, OutputWorkspace=out_ws_name)
                DeleteWorkspaces(wslist)
            else:
                GroupWorkspaces(InputWorkspaces=wslist,
                                OutputWorkspace=out_ws_name)

        # Don't delete workspaces if they were passed in
        if load_van:
            DeleteWorkspace(vanws)

        self.setProperty("OutputWorkspace", out_ws_name)