Exemplo n.º 1
0
    def _get_snapshot_info(self, dialog=False):
        config = conf.get_acqui_conf()

        tab, filepath, exporter = self._main_data_model.tab.value, None, None

        if dialog:
            format_info = get_available_formats()
            wildcards, formats = formats_to_wildcards(format_info)
            # The default file name should be empty because otherwise the
            # dialog will add an extension that won't change when the user
            # selects a different file type in the dialog.
            dlg = wx.FileDialog(self._main_frame,
                                "Save Snapshot",
                                config.last_path,
                                "",
                                wildcard=wildcards,
                                style=wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT)

            # Select the last format used
            try:
                idx = formats.index(config.last_format)
            except ValueError:
                idx = 0
            dlg.SetFilterIndex(idx)

            if dlg.ShowModal() == wx.ID_OK:
                path = dlg.GetPath()
                fmt = formats[dlg.GetFilterIndex()]
                extension = format_info[fmt][0]

                # Prevent double extensions when an old file is selected
                filepath, _ = os.path.splitext(path)
                filepath = filepath + extension

                config.last_path = os.path.dirname(path)
                config.last_format = fmt
                config.last_extension = extension
                config.write()
                exporter = dataio.get_exporter(config.last_format)

            dlg.Destroy()
        else:
            extension = config.last_extension
            dirname = get_picture_folder()
            basename = time.strftime("%Y%m%d-%H%M%S", time.localtime())
            filepath = os.path.join(dirname, basename + extension)
            exporter = dataio.get_exporter(config.last_format)

            if os.path.exists(filepath):
                msg = "File '%s' already exists, cancelling snapshot"
                logging.warning(msg, filepath)
                tab, filepath, exporter = None, None, None

        return tab, filepath, exporter
Exemplo n.º 2
0
 def _export_to_file(self, acq_future):
     """
     return (list of DataArray, filename): data exported and filename
     """
     st = self._tab_data_model.acquisitionView.stream_tree
     thumb = acq.computeThumbnail(st, acq_future)
     data = acq_future.result()
     filename = self.filename.value
     exporter = dataio.get_exporter(self.conf.last_format)
     exporter.export(filename, data, thumb)
     logging.info(u"Acquisition saved as file '%s'.", filename)
     return data, filename
Exemplo n.º 3
0
    def on_acquisition_done(self, future):
        """
        Callback called when the acquisition is finished (either successfully or
        cancelled)
        """
        # bind button back to direct closure
        self.btn_cancel.Bind(wx.EVT_BUTTON, self.on_close)
        self._resume_settings()

        # reenable estimation time updates
        view = self._tab_data_model.focussedView.value
        view.lastUpdate.subscribe(self.on_streams_changed)

        try:
            data = future.result(1) # timeout is just for safety
        except CancelledError:
            # put back to original state:
            # re-enable the acquire button
            self.btn_secom_acquire.Enable()

            # hide progress bar (+ put pack estimated time)
            self.update_acquisition_time()
            self.gauge_acq.Hide()
            self.Layout()
            return
        except Exception:
            # We cannot do much: just warn the user and pretend it was cancelled
            logging.exception("Acquisition failed")
            self.btn_secom_acquire.Enable()
            self.lbl_acqestimate.SetLabel("Acquisition failed.")
            # leave the gauge, to give a hint on what went wrong.
            return

        # save result to file
        self.lbl_acqestimate.SetLabel("Saving file...")
        try:
            thumb = acq.computeThumbnail(
                            self._tab_data_model.focussedView.value.stream_tree,
                            future)
            filename = self.filename.value
            exporter = dataio.get_exporter(self.conf.last_format)
            exporter.export(filename, data, thumb)
            logging.info("Acquisition saved as file '%s'.", filename)
        except Exception:
            logging.exception("Saving acquisition failed")
            self.btn_secom_acquire.Enable()
            self.lbl_acqestimate.SetLabel("Saving acquisition file failed.")
            return

        self.lbl_acqestimate.SetLabel("Acquisition completed.")

        # We don't allow to acquire anymore => change button name
        self.btn_cancel.SetLabel("Close")
Exemplo n.º 4
0
    def test_load_full(self):
        """
        Check the whole sequence: saving calibration data to file, loading it 
        back from file, finding it.
        """
        # AR background data
        dcalib = numpy.zeros((512, 1024), dtype=numpy.uint16)
        md = {
            model.MD_SW_VERSION: "1.0-test",
            model.MD_HW_NAME: "fake ccd",
            model.MD_DESCRIPTION: "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: (1e-6, 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: 60,  # ratio
        }
        calib = model.DataArray(dcalib, md)

        # Give one DA, the correct one, so expect to get it back
        out = calibration.get_ar_data([calib])
        numpy.testing.assert_equal(out, calib)

        # More DataArrays, just to make it slightly harder to find the data
        data1 = model.DataArray(numpy.ones((1, 1, 1, 520, 230),
                                           dtype=numpy.uint16),
                                metadata={model.MD_POS: (1.2e-3, -30e-3)})
        data2 = model.DataArray(17 * numpy.ones((1, 1), dtype=numpy.uint16),
                                metadata={model.MD_POS: (1.2e-3, -30e-3)})
        # RGB image
        thumb = model.DataArray(numpy.ones((520, 230, 3), dtype=numpy.uint8))

        full_data = [data1, calib, data2]

        for fmt in dataio.get_available_formats():
            exporter = dataio.get_exporter(fmt)
            logging.info("Trying to export/import with %s", fmt)
            fn = u"test_ar" + exporter.EXTENSIONS[0]
            exporter.export(fn, full_data, thumb)

            idata = exporter.read_data(fn)
            icalib = calibration.get_ar_data(idata)
            icalib2d = img.ensure2DImage(icalib)
            numpy.testing.assert_equal(icalib2d, calib)
            numpy.testing.assert_almost_equal(
                icalib.metadata[model.MD_AR_POLE],
                calib.metadata[model.MD_AR_POLE])
            os.remove(fn)
Exemplo n.º 5
0
def save_data(data, **kwargs):
    """
    Saves the data into a file
    data (model.DataArray or list of model.DataArray): the data to save
    kwargs (dict (str->value)): values to substitute in the file name
    """
    exporter = dataio.get_exporter(FMT)
    fn = FN_FMT % kwargs

    if os.path.exists(fn):
        # mostly to warn if multiple ypos/xpos are rounded to the same value
        logging.warning("Overwriting file '%s'.", fn)
    else:
        logging.info("Saving file '%s", fn)

    exporter.export(fn, data)
Exemplo n.º 6
0
def save_data(data, **kwargs):
    """
    Saves the data into a file
    data (model.DataArray or list of model.DataArray): the data to save
    kwargs (dict (str->value)): values to substitute in the file name
    """
    exporter = dataio.get_exporter(FMT)
    fn = FN_FMT % kwargs

    if os.path.exists(fn):
        # mostly to warn if multiple ypos/xpos are rounded to the same value
        logging.warning("Overwriting file '%s'.", fn)
    else:
        logging.info("Saving file '%s", fn)

    exporter.export(fn, data)
Exemplo n.º 7
0
    def test_load_full(self):
        """
        Check the whole sequence: saving calibration data to file, loading it 
        back from file, finding it.
        """
        # Compensation data
        dcalib = numpy.array([1, 1.3, 2, 3.5, 4, 5, 0.1, 6, 9.1],
                             dtype=numpy.float)
        dcalib.shape = (dcalib.shape[0], 1, 1, 1, 1)
        wl_calib = 400e-9 + numpy.array(range(dcalib.shape[0])) * 10e-9
        calib = model.DataArray(dcalib, metadata={model.MD_WL_LIST: wl_calib})

        # More DataArrays, just to make it slightly harder to find the data
        data1 = model.DataArray(
            numpy.ones((1, 1, 1, 520, 230), dtype=numpy.uint16))
        data2 = model.DataArray(
            numpy.zeros((3, 1, 1, 520, 230), dtype=numpy.uint16))

        # RGB image
        thumb = model.DataArray(numpy.ones((520, 230, 3), dtype=numpy.uint8))

        full_data = [data1, calib, data2]

        for fmt in dataio.get_available_formats():
            exporter = dataio.get_exporter(fmt)
            logging.info("Trying to export/import with %s", fmt)
            fn = u"test_spec" + exporter.EXTENSIONS[0]
            exporter.export(fn, full_data, thumb)

            idata = exporter.read_data(fn)
            icalib = calibration.get_spectrum_efficiency(idata)
            numpy.testing.assert_equal(icalib, calib)
            numpy.testing.assert_almost_equal(
                icalib.metadata[model.MD_WL_LIST],
                calib.metadata[model.MD_WL_LIST])
            os.remove(fn)
Exemplo n.º 8
0
 def test_get_exporter(self):
     fmts = get_available_formats()
     for fmt in fmts:
         fmt_mng = get_exporter(fmt)
         self.assertGreaterEqual(fmt_mng.EXTENSIONS, 1)
Exemplo n.º 9
0
 def test_get_exporter(self):
     fmts = get_available_formats()
     for fmt in fmts:
         fmt_mng = get_exporter(fmt)
         self.assertGreaterEqual(fmt_mng.EXTENSIONS, 1)