Exemple #1
0
    def test_event_reduction(self):
        # check that eventmode reduction can occur, and that there are the
        # correct number of datasets produced.

        # warnings filter for pixel size
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", RuntimeWarning)

            a = PlatypusReduce(pjoin(self.pth, "PLP0011613.nx.hdf"))

            a.reduce(
                pjoin(self.pth, "PLP0011641.nx.hdf"),
                integrate=0,
                rebin_percent=2,
                eventmode=[0, 900, 1800],
            )

            assert_equal(a.y.shape[0], 2)

            # check that two datasets are written out.
            assert os.path.isfile("PLP0011641_0.dat")
            assert os.path.isfile("PLP0011641_1.dat")

            # check that the resolutions are pretty much the same
            assert_allclose(a.x_err[0] / a.x[0],
                            a.x_err[1] / a.x[1],
                            atol=0.001)

            # check that the (right?) timestamps are written into the datafile
            tree = ET.parse(pjoin(os.getcwd(), "PLP0011641_1.xml"))
            tree.find(".//REFentry").attrib["time"]
    def test_accumulate_files_reduce(self):
        # test by adding a file to itself. Should have smaller stats
        fnames = ['PLP0000708.nx.hdf', 'PLP0000708.nx.hdf']
        pths = [os.path.join(self.pth, fname) for fname in fnames]
        plp.accumulate_HDF_files(pths)

        with warnings.catch_warnings():
            warnings.simplefilter('ignore', RuntimeWarning)
            # it should be processable
            fadd = PlatypusNexus(
                os.path.join(os.getcwd(), 'ADD_PLP0000708.nx.hdf'))
            fadd.process()

            # it should also be reduceable
            reducer = PlatypusReduce(
                os.path.join(self.pth, 'PLP0000711.nx.hdf'))

            datasets, reduced = reducer.reduce(
                os.path.join(os.getcwd(), 'ADD_PLP0000708.nx.hdf'))
            assert_('y' in reduced)

            # the error bars should be smaller
            datasets2, reduced2 = reducer.reduce(
                os.path.join(self.pth, 'PLP0000708.nx.hdf'))

            assert_(np.all(reduced['y_err'] < reduced2['y_err']))
Exemple #3
0
    def test_reduction_method(self):
        # a quick smoke test to check that the reduction can occur
        a = PlatypusReduce('PLP0000711.nx.hdf', data_folder=self.path,
                           rebin_percent=4)

        # try reduction with the reduce method
        a.reduce('PLP0000708.nx.hdf', data_folder=self.path, rebin_percent=4)

        # try reduction with the __call__ method
        a('PLP0000708.nx.hdf', data_folder=self.path, rebin_percent=4)

        # this should also have saved a couple of files in the current
        # directory
        assert_(os.path.isfile('./PLP0000708_0.dat'))
        assert_(os.path.isfile('./PLP0000708_0.xml'))

        # try writing offspecular data
        a.write_offspecular('offspec.xml', 0)
Exemple #4
0
    def test_reduction_method(self):

        # a quick smoke test to check that the reduction can occur
        # warnings filter for pixel size
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", RuntimeWarning)

            a = PlatypusReduce("PLP0000711.nx.hdf", data_folder=self.pth)

            # try reduction with the reduce method
            a.reduce(
                "PLP0000708.nx.hdf",
                data_folder=self.pth,
                rebin_percent=4,
            )

            # try reduction with the __call__ method
            a(
                "PLP0000708.nx.hdf",
                data_folder=self.pth,
                rebin_percent=4,
            )

            # this should also have saved a couple of files in the current
            # directory
            assert os.path.isfile("./PLP0000708_0.dat")
            assert os.path.isfile("./PLP0000708_0.xml")

            # can we read the file
            ReflectDataset("./PLP0000708_0.dat")

            # try writing offspecular data
            a.write_offspecular("offspec.xml", 0)
    def test_accumulate_files_reduce(self):
        # test by adding a file to itself. Should have smaller stats
        fnames = ['PLP0000708.nx.hdf', 'PLP0000708.nx.hdf']
        pths = [os.path.join(self.path, fname) for fname in fnames]
        plp.accumulate_HDF_files(pths)

        # it should be processable
        fadd = PlatypusNexus(os.path.join(os.getcwd(),
                                          'ADD_PLP0000708.nx.hdf'))
        fadd.process()

        # it should also be reduceable
        reducer = PlatypusReduce(os.path.join(self.path,
                                              'PLP0000711.nx.hdf'))
        reduced = reducer.reduce(os.path.join(os.getcwd(),
                                              'ADD_PLP0000708.nx.hdf'))
        assert_('ydata' in reduced)

        # the error bars should be smaller
        reduced2 = reducer.reduce(os.path.join(self.path, 'PLP0000708.nx.hdf'))

        assert_(np.all(reduced['ydata_sd'] < reduced2['ydata_sd']))
Exemple #6
0
    def test_event_reduction(self):
        # check that eventmode reduction can occur, and that there are the
        # correct number of datasets produced.
        a = PlatypusReduce(
            os.path.join(self.pth, 'PLP0011613.nx.hdf'))

        a.reduce(os.path.join(self.pth, 'PLP0011641.nx.hdf'),
                 integrate=0, rebin_percent=2, eventmode=[0, 900, 1800])

        assert_equal(a.y.shape[0], 2)

        # check that two datasets are written out.
        assert_(os.path.isfile('PLP0011641_0.dat'))
        assert_(os.path.isfile('PLP0011641_1.dat'))

        # check that the resolutions are pretty much the same
        assert_allclose(a.x_err[0] / a.x[0],
                        a.x_err[1] / a.x[1],
                        atol=0.001)

        # check that the (right?) timestamps are written into the datafile
        tree = ET.parse(os.path.join(os.getcwd(), 'PLP0011641_1.xml'))
        tree.find('.//REFentry').attrib['time']
Exemple #7
0
    def test_accumulate_files_reduce(self):
        # test by adding a file to itself. Should have smaller stats
        fnames = ["PLP0000708.nx.hdf", "PLP0000708.nx.hdf"]
        pths = [pjoin(self.pth, fname) for fname in fnames]
        plp.accumulate_HDF_files(pths)

        with warnings.catch_warnings():
            warnings.simplefilter("ignore", RuntimeWarning)
            # it should be processable
            fadd = PlatypusNexus(pjoin(os.getcwd(), "ADD_PLP0000708.nx.hdf"))
            fadd.process()

            # it should also be reduceable
            reducer = PlatypusReduce(pjoin(self.pth, "PLP0000711.nx.hdf"))

            datasets, reduced = reducer.reduce(
                pjoin(os.getcwd(), "ADD_PLP0000708.nx.hdf"))
            assert_("y" in reduced)

            # the error bars should be smaller
            datasets2, reduced2 = reducer.reduce(
                pjoin(self.pth, "PLP0000708.nx.hdf"))

            assert_(np.all(reduced["y_err"] < reduced2["y_err"]))
Exemple #8
0
    def test_free_liquids(self):
        # smoke test for free liquids
        a0 = PlatypusReduce('PLP0038418.nx.hdf', data_folder=self.pth)
        a1 = PlatypusReduce('PLP0038417.nx.hdf', data_folder=self.pth)

        # try reduction with the reduce method
        d0, r0 = a0.reduce('PLP0038420.nx.hdf', data_folder=self.pth,
                           rebin_percent=4)
        d1, r1 = a1.reduce('PLP0038421.nx.hdf', data_folder=self.pth,
                           rebin_percent=4)
Exemple #9
0
    def test_reduction_method(self):
        # a quick smoke test to check that the reduction can occur
        a = PlatypusReduce('PLP0000711.nx.hdf', data_folder=self.pth)

        # try reduction with the reduce method
        a.reduce('PLP0000708.nx.hdf', data_folder=self.pth, rebin_percent=4)

        # try reduction with the __call__ method
        a('PLP0000708.nx.hdf', data_folder=self.pth, rebin_percent=4)

        # this should also have saved a couple of files in the current
        # directory
        assert_(os.path.isfile('./PLP0000708_0.dat'))
        assert_(os.path.isfile('./PLP0000708_0.xml'))

        # try writing offspecular data
        a.write_offspecular('offspec.xml', 0)
Exemple #10
0
    def reducer(self, callback=None):
        """
        Reduce all the entries in reduction_entries

        Parameters
        ----------
        callback : callable
            Function, `f(percent_finished)` that is called with the current
            percentage progress of the reduction
        """

        # refnx.reduce.reduce needs you to be in the directory where you're
        # going to write files to
        if self.output_directory:
            os.chdir(self.output_directory)

        # if no data directory was specified then assume it's the cwd
        data_directory = self.data_directory
        if not data_directory:
            data_directory = "./"

        def full_path(fname):
            f = os.path.join(data_directory, fname)
            return f

        # if the streamed directory isn't mentioned then assume it's the same
        # as the data directory
        streamed_directory = self.streamed_directory
        if not os.path.isdir(streamed_directory):
            self.streamed_directory = data_directory

        logging.info("-------------------------------------------------------"
                     "\nStarting reduction run")
        logging.info(
            "data_folder={data_directory}, trim_trailing=True, "
            "lo_wavelength={low_wavelength}, "
            "hi_wavelength={high_wavelength}, "
            "rebin_percent={rebin_percent}, "
            "normalise={monitor_normalisation}, "
            "background={background_subtraction} "
            "eventmode={streamed_reduction} "
            "event_folder={streamed_directory}".format(**self.__dict__))

        # sets up time slices for event reduction
        if self.streamed_reduction:
            eventmode = np.arange(self.stream_start, self.stream_end,
                                  self.stream_duration)
            eventmode = np.r_[eventmode, self.stream_end]
        else:
            eventmode = None

        # are you manual beamfinding?
        peak_pos = None
        if self.manual_beam_find and self.manual_beam_finder is not None:
            peak_pos = -1

        idx = 0

        cached_direct_beams = {}

        for row, val in self.reduction_entries.items():
            if not val["use"]:
                continue

            flood = None
            if val["flood"]:
                flood = full_path(val["flood"])

            combined_dataset = None

            # process entries one by one
            for ref, db in zip(
                ["reflect-1", "reflect-2", "reflect-3"],
                ["direct-1", "direct-2", "direct-3"],
            ):
                reflect = val[ref]
                direct = val[db]

                # if the file doesn't exist there's no point continuing
                if (not os.path.isfile(full_path(reflect))) or (
                        not os.path.isfile(full_path(direct))):
                    continue

                # which of the nspectra to reduce (or all)
                ref_pn = PlatypusNexus(full_path(reflect))

                if direct not in cached_direct_beams:
                    cached_direct_beams[direct] = PlatypusReduce(
                        direct, data_folder=data_directory)

                reducer = cached_direct_beams[direct]

                try:
                    reduced = reducer(
                        ref_pn,
                        scale=val["scale"],
                        h5norm=flood,
                        lo_wavelength=self.low_wavelength,
                        hi_wavelength=self.high_wavelength,
                        rebin_percent=self.rebin_percent,
                        normalise=self.monitor_normalisation,
                        background=self.background_subtraction,
                        manual_beam_find=self.manual_beam_finder,
                        peak_pos=peak_pos,
                        eventmode=eventmode,
                        event_folder=streamed_directory,
                    )
                except Exception as e:
                    # typical Exception would be ValueError for non overlapping
                    # angles
                    logging.info(e)
                    continue

                logging.info("Reduced {} vs {}, scale={}, angle={}".format(
                    reflect,
                    direct,
                    val["scale"],
                    reduced[1]["omega"][0, 0],
                ))

                if combined_dataset is None:
                    combined_dataset = ReflectDataset()

                    fname = basename_datafile(reflect)
                    fname_dat = os.path.join(self.output_directory,
                                             "c_{0}.dat".format(fname))
                    fname_xml = os.path.join(self.output_directory,
                                             "c_{0}.xml".format(fname))

                try:
                    combined_dataset.add_data(
                        reducer.data(),
                        requires_splice=True,
                        trim_trailing=True,
                    )
                except ValueError as e:
                    # datasets don't overlap
                    logging.info(e)
                    continue

            if combined_dataset is not None:
                # after you've finished reducing write a combined file.
                with open(fname_dat, "wb") as f:
                    combined_dataset.save(f)
                with open(fname_xml, "wb") as f:
                    combined_dataset.save_xml(f)
                logging.info("Written combined files: {} and {}".format(
                    fname_dat, fname_xml))

            # can be used to create a progress bar
            idx += 1
            if callback is not None:
                ok = callback(100 * idx / len(self.reduction_entries))
                if not ok:
                    break

        logging.info("\nFinished reduction run"
                     "-------------------------------------------------------")