Example #1
0
    def convert_2d_data(self, dataset):
        metadata = self.get_metadata()
        for key, value in metadata.iteritems():
            setattr(dataset[0], key, value)

        w = NXcanSASWriter()
        w.write(dataset, self.output.GetPath())
Example #2
0
    def convert_2d_data(self, dataset):
        metadata = self.get_metadata()
        for key, value in metadata.iteritems():
            setattr(dataset[0], key, value)

        w = NXcanSASWriter()
        w.write(dataset, self.output.GetPath())
Example #3
0
    def on_convert(self, event):
        """Called when the Convert button is clicked"""
        if not self.validate_inputs():
            return

        self.sample.ID = self.title

        try:
            if self.data_type == 'ascii':
                qdata = self.extract_ascii_data(self.q_input.GetPath())
                iqdata = np.array([self.extract_ascii_data(self.iq_input.GetPath())])
                self.convert_1d_data(qdata, iqdata)
            elif self.data_type == 'otoko':
                qdata, iqdata = self.extract_otoko_data(self.q_input.GetPath())
                self.convert_1d_data(qdata, iqdata)
            else: # self.data_type == 'bsl'
                dataset = self.extract_bsl_data(self.iq_input.GetPath())
                if dataset is None:
                    # Cancelled by user
                    return

                metadata = self.get_metadata()
                for key, value in metadata.iteritems():
                    setattr(dataset[0], key, value)

                w = NXcanSASWriter()
                w.write(dataset, self.output.GetPath())
        except Exception as ex:
            msg = str(ex)
            wx.PostEvent(self.parent.manager.parent,
                StatusEvent(status=msg, info='error'))
            return

        wx.PostEvent(self.parent.manager.parent,
            StatusEvent(status="Conversion completed."))
Example #4
0
class nxcansas_writer(unittest.TestCase):
    def setUp(self):
        self.writer = NXcanSASWriter()
        self.read_file_1d = "cansas1d.xml"
        self.write_file_1d = "export1d.h5"
        self.read_file_2d = "exp18_14_igor_2dqxqy.dat"
        self.write_file_2d = "export2d.h5"
        self.hdf5_reader = HDF5Reader()

        xml_reader = XMLReader()
        self.data_1d = xml_reader.read(self.read_file_1d)[0]

        dat_reader = DATReader()
        self.data_2d = dat_reader.read(self.read_file_2d)
        self.data_2d.detector[0].name = ''
        self.data_2d.source.radiation = 'neutron'

    def test_write_1d(self):
        self.writer.write([self.data_1d], self.write_file_1d)
        data = self.hdf5_reader.read(self.write_file_1d)
        self.assertTrue(len(data) == 1)
        data = data[0]
        self.assertTrue(len(data.x) == len(self.data_1d.x))
        self.assertTrue(len(data.y) == len(self.data_1d.y))
        self.assertTrue(len(data.dy) == len(self.data_1d.dy))
        self._check_metadata(data, self.data_1d)

    def test_write_2d(self):
        self.writer.write([self.data_2d], self.write_file_2d)
        data = self.hdf5_reader.read(self.write_file_2d)
        self.assertTrue(len(data) == 1)
        data = data[0]
        self.assertTrue(len(data.data) == len(self.data_2d.data))
        self.assertTrue(len(data.qx_data) == len(self.data_2d.qx_data))
        self.assertTrue(len(data.qy_data) == len(self.data_2d.qy_data))
        self._check_metadata(data, self.data_2d)

    def _check_metadata(self, written, correct):
        self.assertTrue(written.title == correct.title)
        self.assertTrue(written.sample.name == correct.sample.name)
        self.assertAlmostEqual(written.sample.thickness,
                               correct.sample.thickness)
        self.assertAlmostEqual(written.sample.temperature,
                               correct.sample.temperature)
        self.assertTrue(written.instrument == correct.instrument)
        self.assertTrue(len(written.detector) == len(correct.detector))
        for i in range(len(written.detector)):
            written_det = written.detector[i]
            correct_det = correct.detector[i]
            self.assertAlmostEqual(written_det.distance, correct_det.distance)
            self.assertTrue(written_det.name == correct_det.name)
        self.assertTrue(written.source.radiation == correct.source.radiation)

    def tearDown(self):
        if os.path.isfile(self.write_file_1d):
            os.remove(self.write_file_1d)
        if os.path.isfile(self.write_file_2d):
            os.remove(self.write_file_2d)
class nxcansas_writer(unittest.TestCase):

    def setUp(self):
        self.loader = Loader()
        self.writer = NXcanSASWriter()
        self.read_file_1d = "cansas1d.xml"
        self.write_file_1d = "export1d.h5"
        self.read_file_2d = "exp18_14_igor_2dqxqy.dat"
        self.write_file_2d = "export2d.h5"

        self.data_1d = self.loader.load(self.read_file_1d)[0]

        self.data_2d = self.loader.load(self.read_file_2d)[0]
        self.data_2d.detector[0].name = ''
        self.data_2d.source.radiation = 'neutron'

    def test_write_1d(self):
        self.writer.write([self.data_1d], self.write_file_1d)
        data = self.loader.load(self.write_file_1d)
        self.assertTrue(len(data) == 1)
        data = data[0]
        self.assertTrue(len(data.x) == len(self.data_1d.x))
        self.assertTrue(len(data.y) == len(self.data_1d.y))
        self.assertTrue(len(data.dy) == len(self.data_1d.dy))
        self._check_metadata(data, self.data_1d)

    def test_write_2d(self):
        self.writer.write([self.data_2d], self.write_file_2d)
        data = self.loader.load(self.write_file_2d)
        self.assertTrue(len(data) == 1)
        data = data[0]
        self.assertTrue(len(data.data) == len(self.data_2d.data))
        self.assertTrue(len(data.qx_data) == len(self.data_2d.qx_data))
        self.assertTrue(len(data.qy_data) == len(self.data_2d.qy_data))
        self._check_metadata(data, self.data_2d)

    def _check_metadata(self, written, correct):
        self.assertTrue(written.title == correct.title)
        self.assertTrue(written.sample.name == correct.sample.name)
        self.assertAlmostEqual(written.sample.thickness, correct.sample.thickness)
        self.assertAlmostEqual(written.sample.temperature, correct.sample.temperature)
        self.assertTrue(written.instrument == correct.instrument)
        self.assertTrue(len(written.detector) == len(correct.detector))
        for i in range(len(written.detector)):
            written_det = written.detector[i]
            correct_det = correct.detector[i]
            self.assertAlmostEqual(written_det.distance, correct_det.distance)
            self.assertTrue(written_det.name == correct_det.name)
        self.assertTrue(written.source.radiation == correct.source.radiation)

    def tearDown(self):
        if os.path.isfile(self.write_file_1d):
            os.remove(self.write_file_1d)
        if os.path.isfile(self.write_file_2d):
            os.remove(self.write_file_2d)
    def setUp(self):
        self.loader = Loader()
        self.writer = NXcanSASWriter()
        self.read_file_1d = find("cansas1d.xml")
        self.write_file_1d = find("export1d.h5")
        self.read_file_2d = find("exp18_14_igor_2dqxqy.dat")
        self.write_file_2d = find("export2d.h5")

        self.data_1d = self.loader.load(self.read_file_1d)[0]

        self.data_2d = self.loader.load(self.read_file_2d)[0]
        self.data_2d.detector[0].name = ''
        self.data_2d.source.radiation = 'neutron'
Example #7
0
    def setUp(self):
        self.writer = NXcanSASWriter()
        self.read_file_1d = "cansas1d.xml"
        self.write_file_1d = "export1d.h5"
        self.read_file_2d = "exp18_14_igor_2dqxqy.dat"
        self.write_file_2d = "export2d.h5"
        self.hdf5_reader = HDF5Reader()

        xml_reader = XMLReader()
        self.data_1d = xml_reader.read(self.read_file_1d)[0]

        dat_reader = DATReader()
        self.data_2d = dat_reader.read(self.read_file_2d)
        self.data_2d.detector[0].name = ''
        self.data_2d.source.radiation = 'neutron'
Example #8
0
    def convert_1d_data(self, qdata, iqdata):
        """
        Formats a 1D array of q_axis data and a 2D array of I axis data (where
        each row of iqdata is a separate row), into an array of Data1D objects
        """
        frames = []
        increment = 1
        single_file = True
        n_frames = iqdata.shape[0]
        # Standard file has 3 frames: SAS, calibration and WAS
        if n_frames > 3:
            # File has multiple frames - ask the user which ones they want to
            # export
            params = self.ask_frame_range(n_frames)
            frames = params['frames']
            increment = params['inc']
            single_file = params['file']
            if frames == []: return
        else:  # Only interested in SAS data
            frames = [0]

        output_path = self.output.GetPath()
        metadata = self.get_metadata()

        frame_data = {}
        for i in frames:
            data = Data1D(x=qdata, y=iqdata[i])
            frame_data[i] = data
        if single_file:
            # Only need to set metadata on first Data1D object
            frame_data = frame_data.values(
            )  # Don't need to know frame numbers
            frame_data[0].filename = output_path.split('\\')[-1]
            for key, value in metadata.iteritems():
                setattr(frame_data[0], key, value)
        else:
            # Need to set metadata for all Data1D objects
            for datainfo in frame_data.values():
                datainfo.filename = output_path.split('\\')[-1]
                for key, value in metadata.iteritems():
                    setattr(datainfo, key, value)

        _, ext = os.path.splitext(output_path)
        if ext == '.xml':
            self.convert_to_cansas(frame_data, output_path, single_file)
        else:  # ext == '.h5'
            w = NXcanSASWriter()
            w.write(frame_data, output_path)
Example #9
0
    def convert_1d_data(self, qdata, iqdata):
        """
        Formats a 1D array of q_axis data and a 2D array of I axis data (where
        each row of iqdata is a separate row), into an array of Data1D objects
        """
        frames = []
        increment = 1
        single_file = True
        n_frames = iqdata.shape[0]
        # Standard file has 3 frames: SAS, calibration and WAS
        if n_frames > 3:
            # File has multiple frames - ask the user which ones they want to
            # export
            params = self.ask_frame_range(n_frames)
            frames = params['frames']
            increment = params['inc']
            single_file = params['file']
            if frames == []: return
        else: # Only interested in SAS data
            frames = [0]

        output_path = self.output.GetPath()
        metadata = self.get_metadata()

        frame_data = {}
        for i in frames:
            data = Data1D(x=qdata, y=iqdata[i])
            frame_data[i] = data
        if single_file:
            # Only need to set metadata on first Data1D object
            frame_data = frame_data.values() # Don't need to know frame numbers
            frame_data[0].filename = output_path.split('\\')[-1]
            for key, value in metadata.iteritems():
                setattr(frame_data[0], key, value)
        else:
            # Need to set metadata for all Data1D objects
            for datainfo in frame_data.values():
                datainfo.filename = output_path.split('\\')[-1]
                for key, value in metadata.iteritems():
                    setattr(datainfo, key, value)

        _, ext = os.path.splitext(output_path)
        if ext == '.xml':
            self.convert_to_cansas(frame_data, output_path, single_file)
        else: # ext == '.h5'
            w = NXcanSASWriter()
            w.write(frame_data, output_path)
    def setUp(self):
        self.loader = Loader()
        self.writer = NXcanSASWriter()
        self.read_file_1d = "cansas1d.xml"
        self.write_file_1d = "export1d.h5"
        self.read_file_2d = "exp18_14_igor_2dqxqy.dat"
        self.write_file_2d = "export2d.h5"

        self.data_1d = self.loader.load(self.read_file_1d)[0]

        self.data_2d = self.loader.load(self.read_file_2d)[0]
        self.data_2d.detector[0].name = ''
        self.data_2d.source.radiation = 'neutron'
Example #11
0
    def on_convert(self, event):
        """Called when the Convert button is clicked"""
        if not self.validate_inputs():
            return

        self.sample.ID = self.title

        try:
            if self.data_type == 'ascii':
                qdata = self.extract_ascii_data(self.q_input.GetPath())
                iqdata = np.array(
                    [self.extract_ascii_data(self.iq_input.GetPath())])
                self.convert_1d_data(qdata, iqdata)
            elif self.data_type == 'otoko':
                qdata, iqdata = self.extract_otoko_data(self.q_input.GetPath())
                self.convert_1d_data(qdata, iqdata)
            else:  # self.data_type == 'bsl'
                dataset = self.extract_bsl_data(self.iq_input.GetPath())
                if dataset is None:
                    # Cancelled by user
                    return

                metadata = self.get_metadata()
                for key, value in metadata.iteritems():
                    setattr(dataset[0], key, value)

                w = NXcanSASWriter()
                w.write(dataset, self.output.GetPath())
        except Exception as ex:
            msg = str(ex)
            wx.PostEvent(self.parent.manager.parent,
                         StatusEvent(status=msg, info='error'))
            return

        wx.PostEvent(self.parent.manager.parent,
                     StatusEvent(status="Conversion completed."))