Exemplo n.º 1
0
    def export(self, export_type, raw=False):
        """
        Returns the data to be exported with respect to the settings and options.

        :param export_type (string): spatial, AR, spectrum or spectrum-line
        :param raw (boolean): raw data format if True
        :param interpolate_data (boolean): apply interpolation on data if True

        returns DataArray or list of DataArray: the data to be exported, either
          an RGB image or raw data.
        raises:
            LookupError: if no data found to export
        """
        fview = self._data_model.focussedView.value
        vp = self.get_viewport_by_view(fview)
        streams = fview.getStreams()
        if export_type == 'AR':
            # TODO: set MD_ACQ_TYPE in stream code
            for i, _ in enumerate(streams):
                for r in streams[i].raw:
                    r.metadata[model.MD_ACQ_TYPE] = model.MD_AT_AR
            exported_data = ar_to_export_data(streams, raw)
        elif export_type == 'spectrum':
            spectrum = vp.stream.get_pixel_spectrum()
            spectrum_range, unit = vp.stream.get_spectrum_range()
            spectrum.metadata[model.MD_ACQ_TYPE] = model.MD_AT_SPECTRUM
            exported_data = spectrum_to_export_data(spectrum, raw, unit, spectrum_range)
        elif export_type == 'spectrum-line':
            spectrum_md = vp.stream.get_pixel_spectrum().metadata
            spectrum = vp.stream.get_line_spectrum()
            # copy only non existed values (we mainly care about MD_WL_*)
            spectrum.metadata.update({k: v for k, v in spectrum_md.iteritems() if k not in spectrum.metadata})
            spectrum_range, unit = vp.stream.get_spectrum_range()
            spectrum.metadata[model.MD_ACQ_TYPE] = model.MD_AT_SPECTRUM
            exported_data = line_to_export_data(spectrum, raw, unit, spectrum_range)
        else:
            export_type = 'spatial'
            view_px = tuple(vp.canvas.ClientSize)
            view_mpp = fview.mpp.value
            view_hfw = (view_mpp * view_px[0], view_mpp * view_px[1])
            view_pos = fview.view_pos.value
            draw_merge_ratio = fview.stream_tree.kwargs.get("merge", 0.5)
            interpolate_data = fview.interpolate_content.value
            exported_data = images_to_export_data(streams,
                                                  view_hfw, view_pos,
                                                  draw_merge_ratio, raw,
                                                  interpolate_data=interpolate_data,
                                                  logo=self._main_frame.legend_logo)

        return exported_data
Exemplo n.º 2
0
    def test_ar_export(self):

        filename = "test-ar.csv"

        # Create AR data
        md = {
            model.MD_SW_VERSION: "1.0-test",
            model.MD_HW_NAME: "fake ccd",
            model.MD_DESCRIPTION: "AR",
            model.MD_ACQ_TYPE: model.MD_AT_AR,
            model.MD_ACQ_DATE: time.time(),
            model.MD_BPP: 12,
            model.MD_BINNING: (1, 1),  # px, px
            model.MD_SENSOR_PIXEL_SIZE: (13e-6, 13e-6),  # m/px
            model.MD_PIXEL_SIZE: (2e-5, 2e-5),  # m/px
            model.MD_POS: (1.2e-3, -30e-3),  # m
            model.MD_EXP_TIME: 1.2,  # s
            model.MD_AR_POLE: (253.1, 65.1),
            model.MD_LENS_MAG: 0.4,  # ratio
        }

        md0 = dict(md)
        data0 = model.DataArray(1500 + numpy.zeros((512, 512), dtype=numpy.uint16), md0)
        md1 = dict(md)
        md1[model.MD_POS] = (1.5e-3, -30e-3)
        md1[model.MD_BASELINE] = 300  # AR background should take this into account
        data1 = model.DataArray(3345 + numpy.zeros((512, 512), dtype=numpy.uint16), md1)

        # Create AR stream
        ars = stream.StaticARStream("test", [data0, data1])
        ars.point.value = md1[model.MD_POS]

        # Convert to exportable RGB image
        exdata = img.ar_to_export_data([ars], raw=True)
        # shape = raw data + theta/phi axes values
        self.assertGreater(exdata.shape[0], 50)
        self.assertGreater(exdata.shape[1], 50)

        # Save into a CSV file
        exporter = dataio.get_converter("CSV")
        exporter.export(filename, exdata)
        st = os.stat(filename)  # this test also that the file is created
        self.assertGreater(st.st_size, 100)

        # clean up
        try:
            os.remove(filename)
        except Exception:
            pass
Exemplo n.º 3
0
    def test_ar_export(self):

        filename = "test-ar.csv"

        # Create AR data
        md = {
            model.MD_SW_VERSION: "1.0-test",
            model.MD_HW_NAME: "fake ccd",
            model.MD_DESCRIPTION: "AR",
            model.MD_ACQ_TYPE: model.MD_AT_AR,
            model.MD_ACQ_DATE: time.time(),
            model.MD_BPP: 12,
            model.MD_BINNING: (1, 1),  # px, px
            model.MD_SENSOR_PIXEL_SIZE: (13e-6, 13e-6),  # m/px
            model.MD_PIXEL_SIZE: (2e-5, 2e-5),  # m/px
            model.MD_POS: (1.2e-3, -30e-3),  # m
            model.MD_EXP_TIME: 1.2,  # s
            model.MD_AR_POLE: (253.1, 65.1),
            model.MD_LENS_MAG: 0.4,  # ratio
        }

        md0 = dict(md)
        data0 = model.DataArray(1500 + numpy.zeros((512, 512), dtype=numpy.uint16), md0)
        md1 = dict(md)
        md1[model.MD_POS] = (1.5e-3, -30e-3)
        md1[model.MD_BASELINE] = 300  # AR background should take this into account
        data1 = model.DataArray(3345 + numpy.zeros((512, 512), dtype=numpy.uint16), md1)

        # Create AR stream
        ars = stream.StaticARStream("test", [data0, data1])
        ars.point.value = md1[model.MD_POS]

        # Convert to exportable RGB image
        exdata = img.ar_to_export_data([ars], raw=True)
        # shape = raw data + theta/phi axes values
        self.assertGreater(exdata.shape[0], 50)
        self.assertGreater(exdata.shape[1], 50)

        # Save into a CSV file
        exporter = dataio.get_converter("CSV")
        exporter.export(filename, exdata)
        st = os.stat(filename)  # this test also that the file is created
        self.assertGreater(st.st_size, 100)

        # clean up
        try:
            os.remove(filename)
        except Exception:
            pass
Exemplo n.º 4
0
    def export(self, export_type, raw=False):
        """
        Returns the data to be exported with respect to the settings and options.

        :param export_type (string): spatial, AR, spectrum, spectrum-temporal,
            spectrum-time, or spectrum-line
        :param raw (boolean): raw data format if True
        :param interpolate_data (boolean): apply interpolation on data if True

        returns DataArray or list of DataArray: the data to be exported, either
          an RGB image or raw data.
        raises:
            LookupError: if no data found to export
        """
        fview = self._data_model.focussedView.value
        vp = self.get_viewport_by_view(fview)
        streams = fview.stream_tree.getProjections(
        )  # Stream tree to get the DataProjection
        if export_type == 'AR':
            exported_data = ar_to_export_data(streams, raw)
        elif export_type == 'spectrum':
            exported_data = spectrum_to_export_data(streams[0], raw, vp)
        elif export_type == 'spectrum-line':
            exported_data = line_to_export_data(streams[0], raw)
        elif export_type == 'spectrum-temporal':
            exported_data = temporal_spectrum_to_export_data(streams[0], raw)
        elif export_type == 'spectrum-time':
            exported_data = chronogram_to_export_data(streams[0], raw, vp)
        else:
            view_px = tuple(vp.canvas.ClientSize)
            view_mpp = fview.mpp.value
            view_hfw = (view_mpp * view_px[0], view_mpp * view_px[1])
            view_pos = fview.view_pos.value
            draw_merge_ratio = fview.stream_tree.kwargs.get("merge", 0.5)
            interpolate_data = fview.interpolate_content.value
            exported_data = images_to_export_data(
                streams,
                view_hfw,
                view_pos,
                draw_merge_ratio,
                raw,
                vp.canvas,
                interpolate_data=interpolate_data,
                logo=self._main_frame.legend_logo)

        return exported_data
Exemplo n.º 5
0
def export_ar_to_csv(fn, background=None):
    """
    fn (str): full path to the AR data file
    background (DataArray or None): background data to subtract
    """
    das = dataio.open_acquisition(fn)
    if not das:  # No such file or file doesn't contain data
        return

    streams = dataio.data_to_static_streams(das)

    # Remove the extension of the filename, to extend the name with .csv
    fn_base = dataio.splitext(fn)[0]
    ar_streams = [s for s in streams if isinstance(s, ARStream)]
    for s in ar_streams:
        try:
            s.background.value = background
        except Exception as ex:
            logging.error("Failed to use background data: %s", ex)

        ar_proj = stream.ARRawProjection(s)

        # Export every position separately
        for p in s.point.choices:
            if p == (None,
                     None):  # Special "non-selected point" => not interesting
                continue
            s.point.value = p

            # Project to "raw" = Theta vs phi array
            exdata = img.ar_to_export_data([ar_proj], raw=True)

            # Pick a good name
            fn_csv = fn_base
            if len(ar_streams) > 1:  # Add the name of the stream
                fn_csv += "-" + s.name.value

            if len(s.point.choices) > 2:
                # More than one point in the stream => add position (in µm)
                fn_csv += f"-{p[0] * 1e6}-{p[1] * 1e6}"

            fn_csv += ".csv"

            # Save into a CSV file
            logging.info("Exporting point %s to %s", p, fn_csv)
            csv.export(fn_csv, exdata)
Exemplo n.º 6
0
    def export(self, export_type, raw=False):
        """
        Returns the data to be exported with respect to the settings and options.

        :param export_type (string): spatial, AR, spectrum, spectrum-temporal,
            spectrum-time, or spectrum-line
        :param raw (boolean): raw data format if True
        :param interpolate_data (boolean): apply interpolation on data if True

        returns DataArray or list of DataArray: the data to be exported, either
          an RGB image or raw data.
        raises:
            LookupError: if no data found to export
        """
        fview = self._data_model.focussedView.value
        vp = self.get_viewport_by_view(fview)
        streams = fview.stream_tree.getProjections()  # Stream tree to get the DataProjection
        if export_type == 'AR':
            exported_data = ar_to_export_data(streams, raw)
        elif export_type == 'spectrum':
            exported_data = spectrum_to_export_data(streams[0], raw, vp)
        elif export_type == 'spectrum-line':
            exported_data = line_to_export_data(streams[0], raw)
        elif export_type == 'spectrum-temporal':
            exported_data = temporal_spectrum_to_export_data(streams[0], raw)
        elif export_type == 'spectrum-time':
            exported_data = chronogram_to_export_data(streams[0], raw, vp)
        else:
            view_px = tuple(vp.canvas.ClientSize)
            view_mpp = fview.mpp.value
            view_hfw = (view_mpp * view_px[0], view_mpp * view_px[1])
            view_pos = fview.view_pos.value
            draw_merge_ratio = fview.stream_tree.kwargs.get("merge", 0.5)
            interpolate_data = fview.interpolate_content.value
            exported_data = images_to_export_data(streams,
                                                  view_hfw, view_pos,
                                                  draw_merge_ratio, raw,
                                                  interpolate_data=interpolate_data,
                                                  logo=self._main_frame.legend_logo)

        return exported_data
Exemplo n.º 7
0
    def test_big_ar_export(self):

        # Create AR data
        md = {
            model.MD_SW_VERSION: "1.0-test",
            model.MD_HW_NAME: "fake ccd",
            model.MD_DESCRIPTION: "AR",
            model.MD_ACQ_TYPE: model.MD_AT_AR,
            model.MD_ACQ_DATE: time.time(),
            model.MD_BPP: 12,
            model.MD_BINNING: (1, 1),  # px, px
            model.MD_SENSOR_PIXEL_SIZE: (13e-6, 13e-6),  # m/px
            model.MD_PIXEL_SIZE: (4e-5, 4e-5),  # m/px
            model.MD_POS: (1.2e-3, -30e-3),  # m
            model.MD_EXP_TIME: 1.2,  # s
            model.MD_AR_POLE: (500, 500),
        }

        md0 = dict(md)
        data0 = model.DataArray(1500 + numpy.zeros((1080, 1024), dtype=numpy.uint16), md0)
        md1 = dict(md)
        md1[model.MD_POS] = (1.5e-3, -30e-3)
        md1[model.MD_BASELINE] = 300  # AR background should take this into account
        data1 = model.DataArray(500 + numpy.zeros((1080, 1024), dtype=numpy.uint16), md1)

        # Create AR stream
        ars = stream.StaticARStream("test", [data0])
        ars.point.value = md0[model.MD_POS]

        # Wait for the projection to be computed
        tend = time.time() + 90
        while ars.image.value is None:
            self.assertLess(time.time(), tend, "Timeout during AR computation")
            time.sleep(0.1)

        # Convert to exportable RGB image
        exdata = img.ar_to_export_data([ars], raw=False)
        # shape = RGBA
        self.assertGreater(exdata.shape[0], 200)
        self.assertGreater(exdata.shape[1], 200)
        self.assertEqual(exdata.shape[2], 4)

        # The top-left corner should be white
        numpy.testing.assert_equal(exdata[0, 0], [255, 255, 255, 255])
        # There should be some non-white data
        self.assertTrue(numpy.any(exdata != 255))

        # Save into a PNG file
        exporter = dataio.get_converter("PNG")
        exporter.export(self.FILENAME_PR, exdata)
        st = os.stat(self.FILENAME_PR)  # this test also that the file is created
        self.assertGreater(st.st_size, 1000)

        # Convert to equirectangular (RAW) image
        exdata = img.ar_to_export_data([ars], raw=True)
        # shape = raw data + theta/phi axes values
        self.assertGreater(exdata.shape[0], 50)
        self.assertGreater(exdata.shape[1], 50)

        # Save into a CSV file
        exporter = dataio.get_converter("CSV")
        exporter.export(self.FILENAME_RAW, exdata)
        st = os.stat(self.FILENAME_RAW)  # this test also that the file is created
        self.assertGreater(st.st_size, 100)

        # Create AR stream with background image
        ars.background.value = data1

        # Convert to equirectangular (RAW) image
        exdata = img.ar_to_export_data([ars], raw=True)
        # shape = raw data + theta/phi axes values
        self.assertGreater(exdata.shape[0], 50)
        self.assertGreater(exdata.shape[1], 50)
Exemplo n.º 8
0
    def test_big_ar_export(self):

        # Create AR data
        md = {
            model.MD_SW_VERSION: "1.0-test",
            model.MD_HW_NAME: "fake ccd",
            model.MD_DESCRIPTION: "AR",
            model.MD_ACQ_TYPE: model.MD_AT_AR,
            model.MD_ACQ_DATE: time.time(),
            model.MD_BPP: 12,
            model.MD_BINNING: (1, 1),  # px, px
            model.MD_SENSOR_PIXEL_SIZE: (13e-6, 13e-6),  # m/px
            model.MD_PIXEL_SIZE: (4e-5, 4e-5),  # m/px
            model.MD_POS: (1.2e-3, -30e-3),  # m
            model.MD_EXP_TIME: 1.2,  # s
            model.MD_AR_POLE: (500, 500),
        }

        md0 = dict(md)
        data0 = model.DataArray(
            1500 + numpy.zeros((1080, 1024), dtype=numpy.uint16), md0)
        md1 = dict(md)
        md1[model.MD_POS] = (1.5e-3, -30e-3)
        md1[model.
            MD_BASELINE] = 300  # AR background should take this into account
        data1 = model.DataArray(
            500 + numpy.zeros((1080, 1024), dtype=numpy.uint16), md1)

        # Create AR stream
        ars = stream.StaticARStream("test", [data0])
        ars.point.value = md0[model.MD_POS]

        # Wait for the projection to be computed
        tend = time.time() + 90
        while ars.image.value is None:
            self.assertLess(time.time(), tend, "Timeout during AR computation")
            time.sleep(0.1)

        # Convert to exportable RGB image
        exdata = img.ar_to_export_data([ars], raw=False)
        # shape = RGBA
        self.assertGreater(exdata.shape[0], 200)
        self.assertGreater(exdata.shape[1], 200)
        self.assertEqual(exdata.shape[2], 4)

        # The top-left corner should be white
        numpy.testing.assert_equal(exdata[0, 0], [255, 255, 255, 255])
        # There should be some non-white data
        self.assertTrue(numpy.any(exdata != 255))

        # Save into a PNG file
        exporter = dataio.get_converter("PNG")
        exporter.export(self.FILENAME_PR, exdata)
        st = os.stat(
            self.FILENAME_PR)  # this test also that the file is created
        self.assertGreater(st.st_size, 1000)

        # Convert to equirectangular (RAW) image
        exdata = img.ar_to_export_data([ars], raw=True)
        # shape = raw data + theta/phi axes values
        self.assertGreater(exdata.shape[0], 50)
        self.assertGreater(exdata.shape[1], 50)

        # Save into a CSV file
        exporter = dataio.get_converter("CSV")
        exporter.export(self.FILENAME_RAW, exdata)
        st = os.stat(
            self.FILENAME_RAW)  # this test also that the file is created
        self.assertGreater(st.st_size, 100)

        # Create AR stream with background image
        ars.background.value = data1

        # Convert to equirectangular (RAW) image
        exdata = img.ar_to_export_data([ars], raw=True)
        # shape = raw data + theta/phi axes values
        self.assertGreater(exdata.shape[0], 50)
        self.assertGreater(exdata.shape[1], 50)